1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 package com.android.settings.applications; 15 16 import android.app.Application; 17 import android.content.Context; 18 19 import androidx.annotation.VisibleForTesting; 20 import androidx.preference.Preference; 21 import androidx.preference.PreferenceScreen; 22 23 import com.android.settings.R; 24 import com.android.settings.core.BasePreferenceController; 25 import com.android.settings.datausage.AppStateDataUsageBridge; 26 import com.android.settings.datausage.AppStateDataUsageBridge.DataUsageState; 27 import com.android.settings.datausage.DataSaverBackend; 28 import com.android.settingslib.applications.ApplicationsState; 29 import com.android.settingslib.core.lifecycle.Lifecycle; 30 import com.android.settingslib.core.lifecycle.LifecycleObserver; 31 import com.android.settingslib.core.lifecycle.events.OnDestroy; 32 import com.android.settingslib.core.lifecycle.events.OnStart; 33 import com.android.settingslib.core.lifecycle.events.OnStop; 34 35 import java.util.ArrayList; 36 37 public class SpecialAppAccessPreferenceController extends BasePreferenceController implements 38 AppStateBaseBridge.Callback, ApplicationsState.Callbacks, LifecycleObserver, OnStart, 39 OnStop, OnDestroy { 40 41 @VisibleForTesting 42 ApplicationsState.Session mSession; 43 44 private final ApplicationsState mApplicationsState; 45 private final AppStateDataUsageBridge mDataUsageBridge; 46 private final DataSaverBackend mDataSaverBackend; 47 48 private Preference mPreference; 49 private boolean mExtraLoaded; 50 51 SpecialAppAccessPreferenceController(Context context, String key)52 public SpecialAppAccessPreferenceController(Context context, String key) { 53 super(context, key); 54 mApplicationsState = ApplicationsState.getInstance( 55 (Application) context.getApplicationContext()); 56 mDataSaverBackend = new DataSaverBackend(context); 57 mDataUsageBridge = new AppStateDataUsageBridge(mApplicationsState, this, mDataSaverBackend); 58 } 59 setSession(Lifecycle lifecycle)60 public void setSession(Lifecycle lifecycle) { 61 mSession = mApplicationsState.newSession(this, lifecycle); 62 } 63 64 @Override getAvailabilityStatus()65 public int getAvailabilityStatus() { 66 return AVAILABLE_UNSEARCHABLE; 67 } 68 69 @Override displayPreference(PreferenceScreen screen)70 public void displayPreference(PreferenceScreen screen) { 71 super.displayPreference(screen); 72 mPreference = screen.findPreference(getPreferenceKey()); 73 } 74 75 @Override onStart()76 public void onStart() { 77 mDataUsageBridge.resume(); 78 } 79 80 @Override onStop()81 public void onStop() { 82 mDataUsageBridge.pause(); 83 } 84 85 @Override onDestroy()86 public void onDestroy() { 87 mDataUsageBridge.release(); 88 } 89 90 @Override updateState(Preference preference)91 public void updateState(Preference preference) { 92 updateSummary(); 93 } 94 95 @Override onExtraInfoUpdated()96 public void onExtraInfoUpdated() { 97 mExtraLoaded = true; 98 updateSummary(); 99 } 100 updateSummary()101 private void updateSummary() { 102 if (!mExtraLoaded || mPreference == null) { 103 return; 104 } 105 106 final ArrayList<ApplicationsState.AppEntry> allApps = mSession.getAllApps(); 107 int count = 0; 108 for (ApplicationsState.AppEntry entry : allApps) { 109 if (!ApplicationsState.FILTER_DOWNLOADED_AND_LAUNCHER.filterApp(entry)) { 110 continue; 111 } 112 if (entry.extraInfo instanceof DataUsageState 113 && ((DataUsageState) entry.extraInfo).isDataSaverWhitelisted) { 114 count++; 115 } 116 } 117 mPreference.setSummary(mContext.getResources().getQuantityString( 118 R.plurals.special_access_summary, count, count)); 119 } 120 121 @Override onRunningStateChanged(boolean running)122 public void onRunningStateChanged(boolean running) { 123 } 124 125 @Override onPackageListChanged()126 public void onPackageListChanged() { 127 } 128 129 @Override onRebuildComplete(ArrayList<ApplicationsState.AppEntry> apps)130 public void onRebuildComplete(ArrayList<ApplicationsState.AppEntry> apps) { 131 } 132 133 @Override onPackageIconChanged()134 public void onPackageIconChanged() { 135 } 136 137 @Override onPackageSizeChanged(String packageName)138 public void onPackageSizeChanged(String packageName) { 139 } 140 141 @Override onAllSizesComputed()142 public void onAllSizesComputed() { 143 } 144 145 @Override onLauncherInfoChanged()146 public void onLauncherInfoChanged() { 147 } 148 149 @Override onLoadEntriesCompleted()150 public void onLoadEntriesCompleted() { 151 } 152 153 } 154