-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
35 changed files
with
861 additions
and
397 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# NoLitter | ||
|
||
Prevents apps from littering in storage. |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,21 @@ | ||
package lantian.nolitter | ||
|
||
import android.Manifest | ||
import android.content.pm.PackageManager | ||
import androidx.appcompat.app.AppCompatActivity | ||
import android.os.Bundle | ||
import androidx.core.content.ContextCompat | ||
import androidx.core.app.ActivityCompat | ||
import android.widget.Toast | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.compose.setContent | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.lifecycle.ViewModelProvider | ||
import lantian.nolitter.ui.AppUi | ||
|
||
class MainActivity : AppCompatActivity() { | ||
|
||
class MainActivity : ComponentActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { | ||
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 233) | ||
} | ||
supportFragmentManager.beginTransaction().replace(android.R.id.content, SettingsFragment()).commit() | ||
} | ||
|
||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) { | ||
super.onRequestPermissionsResult(requestCode, permissions, grantResults) | ||
when (requestCode) { | ||
233 -> { | ||
if (!(grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) { | ||
Toast.makeText(this, R.string.ui_failPermission, Toast.LENGTH_SHORT).show() | ||
} | ||
} | ||
} | ||
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean -> | ||
if (!isGranted) Toast.makeText(this, R.string.ui_failPermission, Toast.LENGTH_SHORT).show() | ||
}.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE) | ||
setContent { AppUi(ViewModelProvider(this)[MainViewModel::class.java]) } | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package lantian.nolitter | ||
|
||
import android.app.Application | ||
import android.content.ComponentName | ||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import android.content.pm.ApplicationInfo | ||
import android.content.pm.PackageInfo | ||
import android.content.pm.PackageManager | ||
import android.graphics.drawable.Drawable | ||
import androidx.compose.runtime.MutableState | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.lifecycle.AndroidViewModel | ||
|
||
data class InstalledPackageInfo (val appName: String, val appIcon: Drawable, val isSystem: Boolean, val isModule: Boolean, val isForced: Boolean, val packageName: String) | ||
|
||
class MainViewModel(application: Application) : AndroidViewModel(application) { | ||
private var activity: Application = application | ||
private var sharedPreferences: SharedPreferences? = try { | ||
activity.getSharedPreferences(BuildConfig.APPLICATION_ID + "_preferences", Context.MODE_WORLD_READABLE) | ||
} catch (e: SecurityException) { null } | ||
|
||
var appTheme: MutableState<String?> = mutableStateOf(getStringPreference("theme", "default")) | ||
var topAppBarTitle: MutableState<String> = mutableStateOf(activity.resources.getString(R.string.app_name)) | ||
fun isAvailable(): Boolean { return sharedPreferences != null } | ||
fun getBooleanPreference(key: String, defaultValue: Boolean): Boolean { return sharedPreferences?.getBoolean(key, defaultValue) ?: defaultValue } | ||
fun getStringPreference(key: String, defaultValue: String): String { return sharedPreferences?.getString(key, defaultValue) ?: defaultValue } | ||
fun setBooleanPreference(key: String, value: Boolean) { sharedPreferences!!.edit().putBoolean(key, value).apply() } | ||
fun setStringPreference(key: String, value: String) { sharedPreferences!!.edit().putString(key, value).apply() } | ||
fun hideAppIcon(value: Boolean) { | ||
activity.packageManager.setComponentEnabledSetting( | ||
ComponentName(activity, MainActivity::class.java), | ||
if (value) PackageManager.COMPONENT_ENABLED_STATE_DISABLED else PackageManager.COMPONENT_ENABLED_STATE_ENABLED, | ||
PackageManager.DONT_KILL_APP | ||
) | ||
} | ||
fun getNavigationTitle(key: String?): String { | ||
return when (key) { | ||
"PreferenceGeneral" -> activity.resources.getString(R.string.ui_settings_general) | ||
"PreferenceInterface" -> activity.resources.getString(R.string.ui_settings_interface) | ||
"PreferenceAdvanced" -> activity.resources.getString(R.string.ui_settings_advanced) | ||
"PreferenceSelectApps" -> activity.resources.getString(R.string.ui_settings_forceMode) | ||
else -> activity.resources.getString(R.string.app_name) | ||
} | ||
} | ||
fun getAllInstalledPackages(): ArrayList<InstalledPackageInfo> { | ||
val packageManager = activity.packageManager | ||
val installedPackages: List<PackageInfo> = packageManager.getInstalledPackages(PackageManager.GET_META_DATA) | ||
var allPackageInfo: ArrayList<InstalledPackageInfo> = ArrayList() | ||
val forcedApps = getStringPreference("forced", "").split(",") | ||
for (installedPackage in installedPackages) { | ||
val applicationInfo = installedPackage.applicationInfo | ||
val applicationFlag = installedPackage.applicationInfo.flags | ||
allPackageInfo.add(InstalledPackageInfo( | ||
appName = applicationInfo.loadLabel(packageManager).toString(), | ||
appIcon = applicationInfo.loadIcon(packageManager), | ||
isSystem = (applicationFlag and ApplicationInfo.FLAG_SYSTEM != 0) || (applicationFlag and ApplicationInfo.FLAG_UPDATED_SYSTEM_APP != 0), | ||
isModule = applicationInfo.metaData != null && applicationInfo.metaData.containsKey("xposedmodule"), | ||
isForced = forcedApps.contains(applicationInfo.packageName), | ||
packageName = applicationInfo.packageName, | ||
)) | ||
} | ||
return allPackageInfo | ||
} | ||
fun onChangeForcedApps(packageName: String, newValue: Boolean) { | ||
val forcedApps = getStringPreference("forced", "").split(",").toMutableList() | ||
if (newValue) forcedApps.add(packageName) else forcedApps.remove(packageName) | ||
var newForcedApps = "" | ||
for (forcedApp in forcedApps) { | ||
if (forcedApp.trim().isNotEmpty()) newForcedApps += forcedApp.trim() + "," | ||
} | ||
setStringPreference("forced", newForcedApps) | ||
} | ||
} |
Oops, something went wrong.