Commit 569f056a authored by Mygod's avatar Mygod

Reduce battery usage - register broadcast at runtime

parent 971ea090
...@@ -108,14 +108,6 @@ ...@@ -108,14 +108,6 @@
</intent-filter> </intent-filter>
</service> </service>
<receiver android:name=".AppManagerReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>
<receiver android:name=".ShadowsocksReceiver"> <receiver android:name=".ShadowsocksReceiver">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/> <action android:name="android.intent.action.BOOT_COMPLETED"/>
......
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
package com.github.shadowsocks package com.github.shadowsocks
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.content.{ClipData, ClipboardManager, Context} import android.content._
import android.graphics.PixelFormat import android.graphics.PixelFormat
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.os.{Bundle, Handler} import android.os.{Bundle, Handler}
...@@ -65,13 +65,25 @@ object AppManager { ...@@ -65,13 +65,25 @@ object AppManager {
case class ProxiedApp(name: String, packageName: String, icon: Drawable) case class ProxiedApp(name: String, packageName: String, icon: Drawable)
private case class ListEntry(switch: Switch, text: TextView, icon: ImageView) private case class ListEntry(switch: Switch, text: TextView, icon: ImageView)
var cachedApps: Array[ProxiedApp] = _ private var receiverRegistered: Boolean = _
private var cachedApps: Array[ProxiedApp] = _
private def getApps(pm: PackageManager) = { private def getApps(pm: PackageManager) = {
if (cachedApps == null) cachedApps = pm.getInstalledPackages(PackageManager.GET_PERMISSIONS) if (!receiverRegistered) {
.filter(p => p.requestedPermissions != null && p.requestedPermissions.contains(permission.INTERNET)) val filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED)
.map(p => new ProxiedApp(pm.getApplicationLabel(p.applicationInfo).toString, p.packageName, filter.addAction(Intent.ACTION_PACKAGE_REMOVED)
p.applicationInfo.loadIcon(pm))).toArray filter.addDataScheme("package")
cachedApps ShadowsocksApplication.instance.registerReceiver((context: Context, intent: Intent) =>
if (intent.getAction != Intent.ACTION_PACKAGE_REMOVED || !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false))
synchronized(cachedApps = null), filter)
receiverRegistered = true
}
synchronized {
if (cachedApps == null) cachedApps = pm.getInstalledPackages(PackageManager.GET_PERMISSIONS)
.filter(p => p.requestedPermissions != null && p.requestedPermissions.contains(permission.INTERNET))
.map(p => new ProxiedApp(pm.getApplicationLabel(p.applicationInfo).toString, p.packageName,
p.applicationInfo.loadIcon(pm))).toArray
cachedApps
}
} }
} }
......
package com.github.shadowsocks
import android.content.{Intent, Context, BroadcastReceiver}
/**
* @author Mygod
*/
class AppManagerReceiver extends BroadcastReceiver {
override def onReceive(context: Context, intent: Intent) = if (intent.getAction != Intent.ACTION_PACKAGE_REMOVED ||
!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) AppManager.synchronized(AppManager.cachedApps = null)
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment