1818
1919package com.oasisfeng.nevo.decorators.wechat
2020
21- import android.annotation.SuppressLint
2221import android.app.AlertDialog
2322import android.content.*
2423import android.content.Intent.*
25- import android.content.pm.ApplicationInfo
26- import android.content.pm.LauncherApps
2724import android.content.pm.PackageManager.*
2825import android.net.Uri
2926import android.os.Bundle
30- import android.os.Process
31- import android.os.UserHandle
32- import android.os.UserManager
3327import android.preference.Preference
3428import android.preference.Preference.OnPreferenceClickListener
3529import android.preference.PreferenceActivity
@@ -44,7 +38,7 @@ import com.oasisfeng.nevo.sdk.NevoDecoratorService
4438 */
4539class WeChatDecoratorSettingsActivity : PreferenceActivity () {
4640
47- override fun onCreate (savedInstanceState : Bundle ? ) {
41+ @Deprecated( " Deprecated in Java " ) override fun onCreate (savedInstanceState : Bundle ? ) {
4842 super .onCreate(savedInstanceState)
4943 val manager = preferenceManager
5044 manager.sharedPreferencesName = WeChatDecorator .PREFERENCES_NAME
@@ -77,32 +71,9 @@ class WeChatDecoratorSettingsActivity : PreferenceActivity() {
7771 }
7872 }
7973
80- val isAndroidAutoAvailable = getPackageVersion(ANDROID_AUTO_PACKAGE ) >= 0
81- findPreference(getString(R .string.pref_extension)).apply {
82- val profilesWithoutAndroidAuto = ArrayList <Int >()
83- val profiles = getSystemService(UserManager ::class .java)?.userProfiles ? : emptyList()
84- val la = getSystemService(LauncherApps ::class .java)
85- if (la != null ) for (profile in profiles) {
86- if (profile == Process .myUserHandle()) continue
87- if (la.getApplicationInfo(WECHAT_PACKAGE , profile) == null ) continue
88- if (la.getApplicationInfo(ANDROID_AUTO_PACKAGE , profile) == null )
89- profilesWithoutAndroidAuto.add(profile.hashCode())
90- }
91- isEnabled = isWechatInstalled
92- isSelectable = ! isAndroidAutoAvailable || profilesWithoutAndroidAuto.isNotEmpty()
93- summary = when {
94- ! isAndroidAutoAvailable -> getText(R .string.pref_extension_summary)
95- profilesWithoutAndroidAuto.isEmpty() -> getText(R .string.pref_extension_summary_installed)
96- else -> getString(R .string.pref_extension_summary_not_cloned_in_island,
97- if (profiles.size <= 2 /* Just one Island space */ ) " " else profilesWithoutAndroidAuto.toString()) }
98- onPreferenceClickListener = when {
99- ! isAndroidAutoAvailable -> OnPreferenceClickListener { installExtension() }
100- profilesWithoutAndroidAuto.isNotEmpty() -> OnPreferenceClickListener { showExtensionInIsland() }
101- else -> null }
102- }
103-
10474 val context = this
10575 (findPreference(getString(R .string.pref_compat_mode)) as android.preference.TwoStatePreference ).apply {
76+ val isAndroidAutoAvailable = getPackageVersion(ANDROID_AUTO_PACKAGE ) >= 0
10677 if (isAndroidAutoAvailable && Settings .Global .getInt(contentResolver, Settings .Global .DEVELOPMENT_SETTINGS_ENABLED , 0 ) != 0 ) {
10778 CompatModeController .query(context) { checked: Boolean? -> isChecked = checked!! }
10879 onPreferenceChangeListener = Preference .OnPreferenceChangeListener { _: Preference ? , newValue: Any ->
@@ -114,8 +85,7 @@ class WeChatDecoratorSettingsActivity : PreferenceActivity() {
11485 }
11586
11687 findPreference(getString(R .string.pref_agent))?.apply {
117- var agentVersion = getPackageVersion(AGENT_PACKAGE )
118- if (agentVersion < 0 ) agentVersion = getPackageVersion(AGENT_LEGACY_PACKAGE )
88+ val agentVersion = getPackageVersion(AGENT_PACKAGE )
11989 isEnabled = isWechatInstalled
12090 if (agentVersion >= CURRENT_AGENT_VERSION ) {
12191 val launcherIntent = Intent (ACTION_MAIN ).addCategory(CATEGORY_LAUNCHER )
@@ -130,17 +100,7 @@ class WeChatDecoratorSettingsActivity : PreferenceActivity() {
130100 } else {
131101 setSummary(if (agentVersion < 0 ) R .string.pref_agent_summary else R .string.pref_agent_summary_update)
132102 onPreferenceClickListener = OnPreferenceClickListener {
133- try {
134- pm.getApplicationInfo(AGENT_LEGACY_PACKAGE , 0 )
135- } catch (ignored: NameNotFoundException ) {
136- installAssetApk(" agent.apk" )
137- return @OnPreferenceClickListener true
138- }
139- AlertDialog .Builder (context).setMessage(R .string.prompt_uninstall_agent_first)
140- .setPositiveButton(R .string.action_continue) { _: DialogInterface ? , _: Int ->
141- startActivity(Intent (ACTION_UNINSTALL_PACKAGE , Uri .fromParts(" package" , AGENT_LEGACY_PACKAGE , null ))
142- .putExtra(" android.intent.extra.UNINSTALL_ALL_USERS" , true ))
143- }.show()
103+ startActivity(Intent (ACTION_VIEW , Uri .parse(AGENT_URL )).addFlags(FLAG_ACTIVITY_NEW_TASK ))
144104 true
145105 }
146106 }
@@ -169,41 +129,11 @@ class WeChatDecoratorSettingsActivity : PreferenceActivity() {
169129 return null != mDummyReceiver.peekService(this , service)
170130 }
171131
172- private fun installExtension () = true .also {
173- if (isPlayStoreSystemApp()) {
174- AlertDialog .Builder (this ).setMessage(R .string.prompt_extension_install)
175- .setPositiveButton(R .string.action_install_android_auto) { _: DialogInterface , _: Int -> showAndroidAutoInPlayStore() }
176- .setNeutralButton(R .string.action_install_dummy_auto) { _: DialogInterface , _: Int -> installDummyAuto() }
177- .show()
178- } else installDummyAuto()
179- }
180-
181- @SuppressLint(" InlinedApi" ) private fun showExtensionInIsland () = true .also { try {
182- startActivity(Intent (ACTION_SHOW_APP_INFO ).putExtra(EXTRA_PACKAGE_NAME , ANDROID_AUTO_PACKAGE ).setPackage(ISLAND_PACKAGE ))
183- } catch (e: Exception ) {}}
184-
185- private fun showAndroidAutoInPlayStore () {
186- val uri = Uri .parse(" https://play.google.com/store/apps/details?id=$ANDROID_AUTO_PACKAGE " )
187- try { startActivity(Intent (ACTION_VIEW , uri).setPackage(PLAY_STORE_PACKAGE ).addFlags(FLAG_ACTIVITY_NEW_TASK )) }
188- catch (e: ActivityNotFoundException ) { /* In case of Google Play malfunction */ }
189- }
190-
191- private fun installDummyAuto () {
192- installAssetApk(" dummy-auto.apk" )
193- }
194-
195- private fun installAssetApk (asset_name : String ) {
196- val authority = packageManager.getProviderInfo(ComponentName (this , AssetFileProvider ::class .java), 0 ).authority
197- val uri = Uri .parse(" content://$authority /$asset_name " )
198- try { startActivity(Intent (ACTION_INSTALL_PACKAGE , uri).addFlags(FLAG_GRANT_READ_URI_PERMISSION )) }
199- catch (e: ActivityNotFoundException ) {}
200- }
201-
202132 private fun getPackageVersion (pkg : String ): Int {
203133 return try { packageManager.getPackageInfo(pkg, 0 ).versionCode } catch (e: NameNotFoundException ) { - 1 }
204134 }
205135
206- override fun onDestroy () {
136+ @Deprecated( " Deprecated in Java " ) override fun onDestroy () {
207137 preferenceManager.sharedPreferences.unregisterOnSharedPreferenceChangeListener(mPreferencesChangeListener)
208138 super .onDestroy()
209139 }
@@ -225,10 +155,6 @@ class WeChatDecoratorSettingsActivity : PreferenceActivity() {
225155 return true
226156 }
227157
228- private fun isPlayStoreSystemApp (): Boolean =
229- try { packageManager.getApplicationInfo(PLAY_STORE_PACKAGE , 0 ).flags and ApplicationInfo .FLAG_SYSTEM != 0 }
230- catch (e: NameNotFoundException ) { false }
231-
232158 private val mPreferencesChangeListener = SharedPreferences .OnSharedPreferenceChangeListener { prefs: SharedPreferences , key: String? ->
233159 Log .d(TAG , " Settings changed, notify decorator now." )
234160 sendBroadcast(Intent (ACTION_SETTINGS_CHANGED ).setPackage(packageName).putExtra(key, prefs.getBoolean(key, false )))
@@ -240,12 +166,7 @@ class WeChatDecoratorSettingsActivity : PreferenceActivity() {
240166 private const val CURRENT_AGENT_VERSION = 1700
241167 private const val NEVOLUTION_PACKAGE = " com.oasisfeng.nevo"
242168 private const val ANDROID_AUTO_PACKAGE = " com.google.android.projection.gearhead"
243- private const val PLAY_STORE_PACKAGE = " com.android.vending"
244- private const val ISLAND_PACKAGE = " com.oasisfeng.island"
245169 private const val APP_MARKET_PREFIX = " market://details?id="
246- private const val AGENT_LEGACY_PACKAGE = " com.oasisfeng.nevo.agents.wechat"
247-
248- @SuppressLint(" NewApi" ) private fun LauncherApps.getApplicationInfo (pkg : String , profile : UserHandle ) =
249- try { getApplicationInfo(pkg, 0 , profile) } catch (e: NameNotFoundException ) { null }
170+ private const val AGENT_URL = " https://github.com/Nevolution/decorator-wechat/releases"
250171 }
251172}
0 commit comments