Created
April 21, 2024 14:15
-
-
Save Morteza-QN/abf98ed194ede57ec7eac631deb4bee7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.* | |
import android.net.Uri | |
import android.provider.Settings | |
class ActivityHelper { | |
companion object { | |
@JvmStatic | |
fun startApplicationDetailsActivity(context: Context?, packageName: String?) { | |
// everything is possible... | |
if (packageName == null || packageName.trim().isEmpty()) return | |
try { | |
context?.startActivity( | |
Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply { | |
addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY) | |
addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION) | |
addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION) | |
data = Uri.parse("package:$packageName") | |
} | |
) | |
} catch (e: ActivityNotFoundException) { | |
e.printStackTrace() | |
} | |
} | |
@JvmStatic | |
fun showAccessibilitySettings(context: Context) { | |
val intent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS) | |
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY) | |
context.startActivity(intent) | |
} | |
@JvmStatic | |
fun showUsageAccessSettings(context: Context) { | |
val intent = Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS) | |
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY) | |
context.startActivity(intent) | |
} | |
@JvmStatic | |
fun returnBackToMainActivity(context: Context, intent: Intent) { | |
context.startActivity( | |
intent.apply { | |
addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP) | |
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) | |
addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY) | |
addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION) | |
addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION) | |
} | |
) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ir.adorateb.adorax.common.utils | |
import android.content.Context | |
import java.sql.Date | |
import java.util.* | |
class LocaleHelper { | |
companion object { | |
@JvmStatic | |
fun getCurrentLocale(context: Context): Locale { | |
return context.resources.configuration.locales.get(0) | |
} | |
} | |
data class Location(val latitude: Double, val longitude: Double, val date: Date) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ir.adorateb.adorax.common.utils | |
import android.app.usage.StorageStats | |
import android.app.usage.StorageStatsManager | |
import android.content.Context | |
import android.content.pm.* | |
import android.content.res.Resources | |
import android.graphics.drawable.Drawable | |
import android.os.Build | |
import android.os.storage.StorageManager | |
class PackageManagerHelper { | |
companion object { | |
@JvmStatic | |
fun getInstalledApps( | |
context: Context, | |
systemNotUpdated: Boolean, | |
systemUpdated: Boolean, | |
userOnly: Boolean | |
): ArrayList<PackageInfo> { | |
val list = context.packageManager.getInstalledPackages(0) | |
val pkgInfoList = ArrayList<PackageInfo>() | |
for (i in list.indices) { | |
val packageInfo = list[i] | |
val flags = packageInfo!!.applicationInfo.flags | |
val isSystemApp = (flags and ApplicationInfo.FLAG_SYSTEM) != 0 | |
val isUpdatedSystemApp = (flags and ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0 | |
val addPkg = (systemNotUpdated && (isSystemApp and !isUpdatedSystemApp)) or | |
(systemUpdated && (isSystemApp and isUpdatedSystemApp)) or | |
(userOnly && (!isSystemApp and !isUpdatedSystemApp)) | |
if (addPkg) { | |
pkgInfoList.add(packageInfo) | |
} | |
} | |
return pkgInfoList | |
} | |
@JvmStatic | |
fun getCustomInstalledApps( | |
context: Context, | |
pkgList: Set<String> | |
): ArrayList<PackageInfo> { | |
val list = context.packageManager.getInstalledPackages(0) | |
val pkgInfoList = ArrayList<PackageInfo>() | |
for (i in list.indices) { | |
val packageInfo = list[i] | |
if (pkgList.contains(packageInfo.packageName)) { | |
pkgInfoList.add(packageInfo) | |
} | |
} | |
return pkgInfoList | |
} | |
@JvmStatic | |
fun getApplicationIcon(context: Context, pkgInfo: PackageInfo): Drawable? { | |
return context.packageManager.getApplicationIcon(pkgInfo.packageName) | |
} | |
@JvmStatic | |
fun getApplicationResourceString( | |
context: Context, | |
pkgName: String, | |
resourceName: String | |
): String? { | |
context.packageManager?.let { pm -> | |
try { | |
val res = pm.getResourcesForApplication(pkgName) | |
val resId = res.getIdentifier(resourceName, "string", pkgName) | |
if (resId != 0) { | |
try { | |
return res.getString(resId) | |
} catch (e: Resources.NotFoundException) { | |
e.printStackTrace() | |
} | |
} | |
} catch (e: PackageManager.NameNotFoundException) { | |
e.printStackTrace() | |
} | |
} | |
return null | |
} | |
@JvmStatic | |
fun getApplicationLabel(context: Context, pkgInfo: PackageInfo): String { | |
var localizedLabel: String? = null | |
context.packageManager?.let { pm -> | |
try { | |
val res = pm.getResourcesForApplication(pkgInfo.applicationInfo) | |
val resId = pkgInfo.applicationInfo.labelRes | |
if (resId != 0) { | |
try { | |
localizedLabel = res.getString(resId) | |
} catch (e: Resources.NotFoundException) { | |
e.printStackTrace() | |
} | |
} | |
} catch (e: PackageManager.NameNotFoundException) { | |
e.printStackTrace() | |
} | |
} | |
return localizedLabel | |
?: pkgInfo.applicationInfo.nonLocalizedLabel?.toString() | |
?: pkgInfo.packageName | |
} | |
@JvmStatic | |
fun getStorageStats(context: Context, pkgInfo: PackageInfo): StorageStats? { | |
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return null | |
try { | |
val storageStatsManager = | |
context.getSystemService(Context.STORAGE_STATS_SERVICE) as StorageStatsManager | |
return storageStatsManager.queryStatsForPackage( | |
StorageManager.UUID_DEFAULT, pkgInfo.packageName, | |
android.os.Process.myUserHandle() | |
) | |
} catch (e: Exception) { | |
e.printStackTrace() | |
} | |
return null | |
} | |
@JvmStatic | |
fun getCacheSizeDiff(old: StorageStats?, new: StorageStats?): Long { | |
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return 0 | |
if (old == null || new == null) return 0 | |
try { | |
return if (new.cacheBytes >= old.cacheBytes) { | |
0 | |
} else { | |
old.cacheBytes - new.cacheBytes | |
} | |
} catch (e: Exception) { | |
e.printStackTrace() | |
} | |
return 0 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment