Skip to content

Commit

Permalink
As suggested changes
Browse files Browse the repository at this point in the history
Just copied the systemutils function in core module as dependencies are there on Chat module Util folder and can't be moved to core right now.
  • Loading branch information
AbhishekSingh authored and suhaibroomy committed May 1, 2021
1 parent fb348e5 commit d2ddf6a
Show file tree
Hide file tree
Showing 43 changed files with 219 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.navgurukul.chat.core.error.ChatErrorFormatter
import org.navgurukul.chat.core.pushers.PushersManager
import org.navgurukul.chat.core.repo.*
import org.navgurukul.chat.core.resources.*
import org.merakilearn.core.utils.DimensionConverter
import org.navgurukul.chat.core.utils.DimensionConverter
import org.navgurukul.chat.features.crypto.KeyRequestHandler
import org.navgurukul.chat.features.grouplist.SelectedGroupDataSource
import org.navgurukul.chat.features.home.AvatarRenderer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import androidx.annotation.StringRes
import androidx.core.view.isVisible
import com.google.android.material.snackbar.Snackbar
import org.navgurukul.chat.R
import org.merakilearn.core.utils.copyToClipboard
import org.navgurukul.chat.core.utils.copyToClipboard
import org.navgurukul.commonui.themes.ThemeUtils

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.navgurukul.chat.core.intent
import android.content.Context
import android.net.Uri
import android.webkit.MimeTypeMap
import org.merakilearn.core.utils.getFileExtension
import org.navgurukul.chat.core.utils.getFileExtension
import timber.log.Timber

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package org.navgurukul.chat.core.repo

import arrow.core.Option
import org.matrix.android.sdk.api.session.Session
import org.merakilearn.core.utils.BehaviorDataSource
import org.navgurukul.chat.core.utils.BehaviorDataSource

class ActiveSessionDataSource : BehaviorDataSource<Option<Session>>()
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.navgurukul.chat.R
import org.navgurukul.chat.core.extensions.configureAndStart
import org.merakilearn.core.utils.deleteAllFiles
import org.navgurukul.chat.core.utils.deleteAllFiles
import org.navgurukul.chat.features.settings.ChatPreferences
import org.navgurukul.commonui.resources.StringProvider
import kotlin.coroutines.resume
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.merakilearn.core.utils
package org.navgurukul.chat.core.utils

import android.view.View
import java.util.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.merakilearn.core.utils
package org.navgurukul.chat.core.utils

import android.os.Handler

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.merakilearn.core.utils
package org.navgurukul.chat.core.utils

import io.reactivex.Completable
import io.reactivex.Single
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.merakilearn.core.utils
package org.navgurukul.chat.core.utils

import android.content.res.Resources
import android.util.TypedValue
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.merakilearn.core.utils
package org.navgurukul.chat.core.utils

import java.util.regex.Pattern

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.merakilearn.core.utils
package org.navgurukul.chat.core.utils

import android.text.Spanned
import android.text.style.ClickableSpan
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.merakilearn.core.utils
package org.navgurukul.chat.core.utils

import android.content.ActivityNotFoundException
import android.content.Context
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.merakilearn.core.utils
package org.navgurukul.chat.core.utils

import timber.log.Timber
import java.io.File
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

package org.merakilearn.core.utils
package org.navgurukul.chat.core.utils

import android.os.Handler
import android.os.HandlerThread
Expand Down
164 changes: 164 additions & 0 deletions chat/src/main/java/org/navgurukul/chat/core/utils/SystemUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package org.navgurukul.chat.core.utils

import android.annotation.TargetApi
import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.PowerManager
import android.provider.Settings
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import org.navgurukul.chat.R
import org.navgurukul.chat.features.notifications.NotificationUtils

/**
* Tells if the application ignores battery optimizations.
*
* Ignoring them allows the app to run in background to make background sync with the homeserver.
* This user option appears on Android M but Android O enforces its usage and kills apps not
* authorised by the user to run in background.
*
* @param context the context
* @return true if battery optimisations are ignored
*/
fun isIgnoringBatteryOptimizations(context: Context): Boolean {
// no issue before Android M, battery optimisations did not exist
return Build.VERSION.SDK_INT < Build.VERSION_CODES.M
|| (context.getSystemService(Context.POWER_SERVICE) as PowerManager?)?.isIgnoringBatteryOptimizations(context.packageName) == true
}

fun isAirplaneModeOn(context: Context): Boolean {
return Settings.Global.getInt(context.contentResolver, Settings.Global.AIRPLANE_MODE_ON, 0) != 0
}

/**
* display the system dialog for granting this permission. If previously granted, the
* system will not show it (so you should call this method).
*
* Note: If the user finally does not grant the permission, PushManager.isBackgroundSyncAllowed()
* will return false and the notification privacy will fallback to "LOW_DETAIL".
*/
@TargetApi(Build.VERSION_CODES.M)
fun requestDisablingBatteryOptimization(activity: Activity, fragment: Fragment?, requestCode: Int) {
val intent = Intent()
intent.action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
intent.data = Uri.parse("package:" + activity.packageName)
if (fragment != null) {
fragment.startActivityForResult(intent, requestCode)
} else {
activity.startActivityForResult(intent, requestCode)
}
}

// ==============================================================================================================
// Clipboard helper
// ==============================================================================================================

/**
* Copy a text to the clipboard, and display a Toast when done
*
* @param context the context
* @param text the text to copy
*/
fun copyToClipboard(context: Context, text: CharSequence, showToast: Boolean = true, @StringRes toastMessage: Int = R.string.copied_to_clipboard) {
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
clipboard.setPrimaryClip(ClipData.newPlainText("", text))
if (showToast) {
context.toast(toastMessage)
}
}

/**
* Shows notification settings for the current app.
* In android O will directly opens the notification settings, in lower version it will show the App settings
*/
fun startNotificationSettingsIntent(activity: AppCompatActivity, requestCode: Int) {
val intent = Intent()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
intent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
intent.putExtra(Settings.EXTRA_APP_PACKAGE, activity.packageName)
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
intent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
intent.putExtra("app_package", activity.packageName)
intent.putExtra("app_uid", activity.applicationInfo?.uid)
} else {
intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
intent.addCategory(Intent.CATEGORY_DEFAULT)
val uri = Uri.fromParts("package", activity.packageName, null)
intent.data = uri
}
activity.startActivityForResult(intent, requestCode)
}

/**
* Shows notification system settings for the given channel id.
*/
@TargetApi(Build.VERSION_CODES.O)
fun startNotificationChannelSettingsIntent(fragment: Fragment, channelID: String) {
if (!NotificationUtils.supportNotificationChannels()) return
val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS).apply {
putExtra(Settings.EXTRA_APP_PACKAGE, fragment.context?.packageName)
putExtra(Settings.EXTRA_CHANNEL_ID, channelID)
}
fragment.startActivity(intent)
}

fun startAddGoogleAccountIntent(context: AppCompatActivity, requestCode: Int) {
try {
val intent = Intent(Settings.ACTION_ADD_ACCOUNT)
intent.putExtra(Settings.EXTRA_ACCOUNT_TYPES, arrayOf("com.google"))
context.startActivityForResult(intent, requestCode)
} catch (activityNotFoundException: ActivityNotFoundException) {
context.toast(R.string.error_no_external_application_found)
}
}

fun startSharePlainTextIntent(fragment: Fragment, chooserTitle: String?, text: String, subject: String? = null, requestCode: Int? = null) {
val share = Intent(Intent.ACTION_SEND)
share.type = "text/plain"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
share.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT)
} else {
share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
}
// Add data to the intent, the receiving app will decide what to do with it.
share.putExtra(Intent.EXTRA_SUBJECT, subject)
share.putExtra(Intent.EXTRA_TEXT, text)
try {
if (requestCode != null) {
fragment.startActivityForResult(Intent.createChooser(share, chooserTitle), requestCode)
} else {
fragment.startActivity(Intent.createChooser(share, chooserTitle))
}
} catch (activityNotFoundException: ActivityNotFoundException) {
fragment.activity?.toast(R.string.error_no_external_application_found)
}
}

fun startImportTextFromFileIntent(fragment: Fragment, requestCode: Int) {
val intent = Intent(Intent.ACTION_GET_CONTENT).apply {
type = "text/plain"
}
if (intent.resolveActivity(fragment.requireActivity().packageManager) != null) {
fragment.startActivityForResult(intent, requestCode)
} else {
fragment.activity?.toast(R.string.error_no_external_application_found)
}
}

// Not in KTX anymore
fun Context.toast(resId: Int) {
Toast.makeText(this, resId, Toast.LENGTH_SHORT).show()
}

// Not in KTX anymore
fun Context.toast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.merakilearn.core.utils
package org.navgurukul.chat.core.utils

import android.content.Context
import android.os.Build
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.merakilearn.core.utils
package org.navgurukul.chat.core.utils

import java.net.URL

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.merakilearn.core.utils
package org.navgurukul.chat.core.utils

import androidx.annotation.ColorRes
import org.navgurukul.chat.R
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package org.navgurukul.chat.features.grouplist

import arrow.core.Option
import org.matrix.android.sdk.api.session.group.model.GroupSummary
import org.merakilearn.core.utils.BehaviorDataSource
import org.navgurukul.chat.core.utils.BehaviorDataSource

class SelectedGroupDataSource : BehaviorDataSource<Option<GroupSummary>>(Option.empty())
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import org.navgurukul.chat.core.glide.GlideApp
import org.navgurukul.chat.core.glide.GlideRequest
import org.navgurukul.chat.core.glide.GlideRequests
import org.navgurukul.chat.core.repo.ActiveSessionHolder
import org.merakilearn.core.utils.getColorFromRoomId
import org.merakilearn.core.utils.getColorFromUserId
import org.navgurukul.chat.core.utils.getColorFromRoomId
import org.navgurukul.chat.core.utils.getColorFromUserId

/**
* This helper centralise ways to retrieve avatar into ImageView or even generic Target<Drawable>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.navgurukul.chat.features.home

import org.matrix.android.sdk.api.session.room.model.RoomSummary
import org.merakilearn.core.utils.BehaviorDataSource
import org.navgurukul.chat.core.utils.BehaviorDataSource

class HomeRoomListDataSource : BehaviorDataSource<List<RoomSummary>>()
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.navgurukul.chat.features.home.room.detail
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.floatingactionbutton.FloatingActionButton
import org.merakilearn.core.utils.Debouncer
import org.navgurukul.chat.core.utils.Debouncer

/**
* Show or hide the jumpToBottomView, depending on the scrolling and if the timeline is displaying the more recent event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ import org.navgurukul.chat.core.dialogs.withColoredButton
import org.navgurukul.chat.core.extensions.*
import org.navgurukul.chat.core.glide.GlideApp
import org.navgurukul.chat.core.repo.ActiveSessionHolder
import org.merakilearn.core.utils.*
import org.merakilearn.core.utils.createUIHandler
import org.navgurukul.chat.core.utils.*
import org.navgurukul.chat.core.utils.createUIHandler
import org.navgurukul.chat.core.views.NotificationAreaView
import org.navgurukul.chat.features.home.AvatarRenderer
import org.navgurukul.chat.features.home.room.detail.composer.TextComposerView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ import org.matrix.android.sdk.api.session.room.timeline.*
import org.navgurukul.chat.R
import org.navgurukul.chat.core.repo.ActiveSessionHolder
import org.navgurukul.chat.core.resources.UserPreferencesProvider
import org.merakilearn.core.utils.isValidUrl
import org.merakilearn.core.utils.subscribeLogError
import org.navgurukul.chat.core.utils.isValidUrl
import org.navgurukul.chat.core.utils.subscribeLogError
import org.navgurukul.chat.features.command.CommandParser
import org.navgurukul.chat.features.command.ParsedCommand
import org.navgurukul.chat.features.home.room.TypingHelper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import androidx.annotation.ColorInt
import org.matrix.android.sdk.api.session.room.send.SendState
import org.navgurukul.chat.R
import org.navgurukul.chat.core.resources.ColorProvider
import org.merakilearn.core.utils.getColorFromUserId
import org.navgurukul.chat.core.utils.getColorFromUserId

class MessageColorProvider(
private val colorProvider: ColorProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import org.navgurukul.chat.core.extensions.KEY_ARG
import org.navgurukul.chat.core.extensions.args
import org.navgurukul.chat.core.extensions.cleanup
import org.navgurukul.chat.core.extensions.configureWith
import org.merakilearn.core.utils.DimensionConverter
import org.navgurukul.chat.core.utils.DimensionConverter
import org.navgurukul.chat.features.home.room.detail.timeline.item.MessageInformationData

@Parcelize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import org.navgurukul.chat.R
import org.navgurukul.chat.core.epoxy.MerakiEpoxyModel
import org.navgurukul.chat.core.repo.ActiveSessionHolder
import org.navgurukul.chat.core.resources.ColorProvider
import org.merakilearn.core.utils.DebouncedClickListener
import org.merakilearn.core.utils.DimensionConverter
import org.merakilearn.core.utils.containsOnlyEmojis
import org.merakilearn.core.utils.isLocalFile
import org.navgurukul.chat.core.utils.DebouncedClickListener
import org.navgurukul.chat.core.utils.DimensionConverter
import org.navgurukul.chat.core.utils.containsOnlyEmojis
import org.navgurukul.chat.core.utils.isLocalFile
import org.navgurukul.chat.features.home.room.detail.timeline.TimelineEventController
import org.navgurukul.chat.features.home.room.detail.timeline.helper.*
import org.navgurukul.chat.features.home.room.detail.timeline.item.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.navgurukul.chat.features.home.room.detail.timeline.helper

import org.merakilearn.core.utils.DimensionConverter
import org.navgurukul.chat.core.utils.DimensionConverter

class AvatarSizeProvider(private val dimensionConverter: DimensionConverter) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.matrix.android.sdk.api.session.content.ContentUploadStateTracker
import org.matrix.android.sdk.api.session.room.send.SendState
import org.navgurukul.chat.R
import org.navgurukul.chat.core.repo.ActiveSessionHolder
import org.merakilearn.core.utils.TextUtils.formatFileSize
import org.navgurukul.chat.core.utils.TextUtils.formatFileSize
import org.navgurukul.chat.features.home.room.detail.timeline.MessageColorProvider
import org.navgurukul.commonui.error.ErrorFormatter

Expand Down
Loading

0 comments on commit d2ddf6a

Please sign in to comment.