1 /* 2 * Copyright (C) 2016 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 15 package com.android.settings.datausage; 16 17 import android.app.Application; 18 import android.app.settings.SettingsEnums; 19 import android.content.Context; 20 import android.os.Bundle; 21 import android.telephony.SubscriptionManager; 22 import android.widget.Switch; 23 24 import androidx.preference.Preference; 25 26 import com.android.settings.R; 27 import com.android.settings.SettingsActivity; 28 import com.android.settings.SettingsPreferenceFragment; 29 import com.android.settings.applications.AppStateBaseBridge.Callback; 30 import com.android.settings.datausage.DataSaverBackend.Listener; 31 import com.android.settings.search.BaseSearchIndexProvider; 32 import com.android.settings.widget.SettingsMainSwitchBar; 33 import com.android.settingslib.applications.ApplicationsState; 34 import com.android.settingslib.applications.ApplicationsState.AppEntry; 35 import com.android.settingslib.applications.ApplicationsState.Callbacks; 36 import com.android.settingslib.applications.ApplicationsState.Session; 37 import com.android.settingslib.search.SearchIndexable; 38 import com.android.settingslib.widget.OnMainSwitchChangeListener; 39 40 import java.util.ArrayList; 41 42 @SearchIndexable 43 public class DataSaverSummary extends SettingsPreferenceFragment 44 implements OnMainSwitchChangeListener, Listener, Callback, Callbacks { 45 46 private static final String KEY_UNRESTRICTED_ACCESS = "unrestricted_access"; 47 48 private SettingsMainSwitchBar mSwitchBar; 49 private DataSaverBackend mDataSaverBackend; 50 private Preference mUnrestrictedAccess; 51 private ApplicationsState mApplicationsState; 52 private AppStateDataUsageBridge mDataUsageBridge; 53 private Session mSession; 54 55 // Flag used to avoid infinite loop due if user switch it on/off too quicky. 56 private boolean mSwitching; 57 58 @Override onCreate(Bundle icicle)59 public void onCreate(Bundle icicle) { 60 super.onCreate(icicle); 61 62 addPreferencesFromResource(R.xml.data_saver); 63 mUnrestrictedAccess = findPreference(KEY_UNRESTRICTED_ACCESS); 64 mApplicationsState = ApplicationsState.getInstance( 65 (Application) getContext().getApplicationContext()); 66 mDataSaverBackend = new DataSaverBackend(getContext()); 67 mDataUsageBridge = new AppStateDataUsageBridge(mApplicationsState, this, mDataSaverBackend); 68 mSession = mApplicationsState.newSession(this, getSettingsLifecycle()); 69 } 70 71 @Override onActivityCreated(Bundle savedInstanceState)72 public void onActivityCreated(Bundle savedInstanceState) { 73 super.onActivityCreated(savedInstanceState); 74 mSwitchBar = ((SettingsActivity) getActivity()).getSwitchBar(); 75 mSwitchBar.setTitle(getContext().getString(R.string.data_saver_switch_title)); 76 mSwitchBar.show(); 77 mSwitchBar.addOnSwitchChangeListener(this); 78 } 79 80 @Override onResume()81 public void onResume() { 82 super.onResume(); 83 mDataSaverBackend.refreshAllowlist(); 84 mDataSaverBackend.refreshDenylist(); 85 mDataSaverBackend.addListener(this); 86 mDataUsageBridge.resume(); 87 } 88 89 @Override onPause()90 public void onPause() { 91 super.onPause(); 92 mDataSaverBackend.remListener(this); 93 mDataUsageBridge.pause(); 94 } 95 96 @Override onSwitchChanged(Switch switchView, boolean isChecked)97 public void onSwitchChanged(Switch switchView, boolean isChecked) { 98 synchronized (this) { 99 if (mSwitching) { 100 return; 101 } 102 mSwitching = true; 103 mDataSaverBackend.setDataSaverEnabled(isChecked); 104 } 105 } 106 107 @Override getMetricsCategory()108 public int getMetricsCategory() { 109 return SettingsEnums.DATA_SAVER_SUMMARY; 110 } 111 112 @Override getHelpResource()113 public int getHelpResource() { 114 return R.string.help_url_data_saver; 115 } 116 117 @Override onDataSaverChanged(boolean isDataSaving)118 public void onDataSaverChanged(boolean isDataSaving) { 119 synchronized (this) { 120 mSwitchBar.setChecked(isDataSaving); 121 mSwitching = false; 122 } 123 } 124 125 @Override onAllowlistStatusChanged(int uid, boolean isAllowlisted)126 public void onAllowlistStatusChanged(int uid, boolean isAllowlisted) { 127 } 128 129 @Override onDenylistStatusChanged(int uid, boolean isDenylisted)130 public void onDenylistStatusChanged(int uid, boolean isDenylisted) { 131 } 132 133 @Override onExtraInfoUpdated()134 public void onExtraInfoUpdated() { 135 if (!isAdded()) { 136 return; 137 } 138 int count = 0; 139 final ArrayList<AppEntry> allApps = mSession.getAllApps(); 140 final int N = allApps.size(); 141 for (int i = 0; i < N; i++) { 142 final AppEntry entry = allApps.get(i); 143 if (!ApplicationsState.FILTER_DOWNLOADED_AND_LAUNCHER.filterApp(entry)) { 144 continue; 145 } 146 if (entry.extraInfo != null && ((AppStateDataUsageBridge.DataUsageState) 147 entry.extraInfo).isDataSaverAllowlisted) { 148 count++; 149 } 150 } 151 mUnrestrictedAccess.setSummary(getResources().getQuantityString( 152 R.plurals.data_saver_unrestricted_summary, count, count)); 153 } 154 155 @Override onRunningStateChanged(boolean running)156 public void onRunningStateChanged(boolean running) { 157 158 } 159 160 @Override onPackageListChanged()161 public void onPackageListChanged() { 162 163 } 164 165 @Override onRebuildComplete(ArrayList<AppEntry> apps)166 public void onRebuildComplete(ArrayList<AppEntry> apps) { 167 168 } 169 170 @Override onPackageIconChanged()171 public void onPackageIconChanged() { 172 173 } 174 175 @Override onPackageSizeChanged(String packageName)176 public void onPackageSizeChanged(String packageName) { 177 178 } 179 180 @Override onAllSizesComputed()181 public void onAllSizesComputed() { 182 183 } 184 185 @Override onLauncherInfoChanged()186 public void onLauncherInfoChanged() { 187 188 } 189 190 @Override onLoadEntriesCompleted()191 public void onLoadEntriesCompleted() { 192 193 } 194 195 public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 196 new BaseSearchIndexProvider(R.xml.data_saver) { 197 198 @Override 199 protected boolean isPageSearchEnabled(Context context) { 200 return DataUsageUtils.hasMobileData(context) 201 && DataUsageUtils.getDefaultSubscriptionId(context) 202 != SubscriptionManager.INVALID_SUBSCRIPTION_ID; 203 } 204 }; 205 } 206