@Override public void onCreate(Bundle icicle) { super.onCreate(icicle); final Context context = getActivity(); mPolicyManager = NetworkPolicyManager.from(context); /// M: WifiManager memory leak , change context to getApplicationContext @{ mWifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); /// @} mPolicyEditor = new NetworkPolicyEditor(mPolicyManager); mPolicyEditor.read(); addPreferencesFromResource(R.xml.data_usage_metered_prefs); mMobileCategory = (PreferenceCategory) findPreference("mobile"); mWifiCategory = (PreferenceCategory) findPreference("wifi"); mWifiDisabled = findPreference("wifi_disabled"); updateNetworks(context); }
@Override public void onClick(DialogInterface dialog, int which) { if (mResetDialog == dialog) { final PackageManager pm = getActivity().getPackageManager(); final INotificationManager nm = INotificationManager.Stub.asInterface( ServiceManager.getService(Context.NOTIFICATION_SERVICE)); final NetworkPolicyManager npm = NetworkPolicyManager.from(getActivity()); final Handler handler = new Handler(getActivity().getMainLooper()); (new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... params) { List<ApplicationInfo> apps = pm.getInstalledApplications(PackageManager.GET_DISABLED_COMPONENTS); for (int i = 0; i < apps.size(); i++) { ApplicationInfo app = apps.get(i); try { if (DEBUG) Log.v(TAG, "Enabling notifications: " + app.packageName); nm.setNotificationsEnabledForPackage(app.packageName, true); } catch (android.os.RemoteException ex) { } if (DEBUG) Log.v(TAG, "Clearing preferred: " + app.packageName); pm.clearPackagePreferredActivities(app.packageName); if (!app.enabled) { if (DEBUG) Log.v(TAG, "Enabling app: " + app.packageName); if (pm.getApplicationEnabledSetting(app.packageName) == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER) { pm.setApplicationEnabledSetting( app.packageName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP); } } } // We should have cleared all of the preferred apps above; // just in case some may be lingering, retrieve whatever is // still set and remove it. ArrayList<IntentFilter> filters = new ArrayList<IntentFilter>(); ArrayList<ComponentName> prefActivities = new ArrayList<ComponentName>(); pm.getPreferredActivities(filters, prefActivities, null); for (int i = 0; i < prefActivities.size(); i++) { if (DEBUG) Log.v(TAG, "Clearing preferred: " + prefActivities.get(i).getPackageName()); pm.clearPackagePreferredActivities(prefActivities.get(i).getPackageName()); } final int[] restrictedUids = npm.getUidsWithPolicy(POLICY_REJECT_METERED_BACKGROUND); final int currentUserId = ActivityManager.getCurrentUser(); for (int uid : restrictedUids) { // Only reset for current user if (UserHandle.getUserId(uid) == currentUserId) { if (DEBUG) Log.v(TAG, "Clearing data policy: " + uid); npm.setUidPolicy(uid, POLICY_NONE); } } handler.post( new Runnable() { @Override public void run() { if (DEBUG) Log.v(TAG, "Done clearing"); if (getActivity() != null && mActivityResumed) { if (DEBUG) Log.v(TAG, "Updating UI!"); for (int i = 0; i < mTabs.size(); i++) { TabInfo tab = mTabs.get(i); if (tab.mApplications != null) { tab.mApplications.pause(); } } if (mCurTab != null) { mCurTab.resume(mSortOrder); } } } }); return null; } }) .execute(); } }
@Override public void onClick(DialogInterface dialog, int which) { if (mResetDialog == dialog) { final PackageManager pm = getActivity().getPackageManager(); final IPackageManager mIPm = IPackageManager.Stub.asInterface(ServiceManager.getService("package")); final INotificationManager nm = INotificationManager.Stub.asInterface( ServiceManager.getService(Context.NOTIFICATION_SERVICE)); final NetworkPolicyManager npm = NetworkPolicyManager.from(getActivity()); final AppOpsManager aom = (AppOpsManager) getActivity().getSystemService(Context.APP_OPS_SERVICE); final Handler handler = new Handler(getActivity().getMainLooper()); (new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... params) { List<ApplicationInfo> apps = pm.getInstalledApplications(PackageManager.GET_DISABLED_COMPONENTS); for (int i = 0; i < apps.size(); i++) { ApplicationInfo app = apps.get(i); try { if (DEBUG) Log.v(TAG, "Enabling notifications: " + app.packageName); nm.setNotificationsEnabledForPackage(app.packageName, app.uid, true); } catch (android.os.RemoteException ex) { } if (!app.enabled) { if (DEBUG) Log.v(TAG, "Enabling app: " + app.packageName); if (pm.getApplicationEnabledSetting(app.packageName) == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER) { pm.setApplicationEnabledSetting( app.packageName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP); } } } try { mIPm.resetPreferredActivities(UserHandle.myUserId()); } catch (RemoteException e) { } aom.resetAllModes(); final int[] restrictedUids = npm.getUidsWithPolicy(POLICY_REJECT_METERED_BACKGROUND); final int currentUserId = ActivityManager.getCurrentUser(); for (int uid : restrictedUids) { // Only reset for current user if (UserHandle.getUserId(uid) == currentUserId) { if (DEBUG) Log.v(TAG, "Clearing data policy: " + uid); npm.setUidPolicy(uid, POLICY_NONE); } } handler.post( new Runnable() { @Override public void run() { if (DEBUG) Log.v(TAG, "Done clearing"); if (getActivity() != null && mActivityResumed) { if (DEBUG) Log.v(TAG, "Updating UI!"); for (int i = 0; i < mTabs.size(); i++) { TabInfo tab = mTabs.get(i); if (tab.mApplications != null) { tab.mApplications.pause(); } } if (mCurTab != null) { mCurTab.resume(mSortOrder); } } } }); return null; } }) .execute(); } }