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