Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.settings; |
| 18 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 19 | import static android.net.NetworkPolicy.LIMIT_DISABLED; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 20 | import static android.net.NetworkPolicyManager.computeLastCycleBoundary; |
| 21 | import static android.net.NetworkPolicyManager.computeNextCycleBoundary; |
| 22 | import static android.net.TrafficStats.TEMPLATE_MOBILE_3G_LOWER; |
| 23 | import static android.net.TrafficStats.TEMPLATE_MOBILE_4G; |
| 24 | import static android.net.TrafficStats.TEMPLATE_MOBILE_ALL; |
| 25 | import static android.net.TrafficStats.TEMPLATE_WIFI; |
| 26 | import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 27 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 28 | import android.app.AlertDialog; |
| 29 | import android.app.Dialog; |
| 30 | import android.app.DialogFragment; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 31 | import android.app.Fragment; |
| 32 | import android.content.Context; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 33 | import android.content.DialogInterface; |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 34 | import android.content.Intent; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 35 | import android.content.pm.PackageManager; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 36 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 37 | import android.net.INetworkStatsService; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 38 | import android.net.NetworkPolicy; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 39 | import android.net.NetworkStats; |
| 40 | import android.net.NetworkStatsHistory; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 41 | import android.os.Bundle; |
| 42 | import android.os.RemoteException; |
| 43 | import android.os.ServiceManager; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 44 | import android.preference.CheckBoxPreference; |
| 45 | import android.preference.Preference; |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 46 | import android.preference.PreferenceActivity; |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 47 | import android.preference.SwitchPreference; |
| 48 | import android.telephony.TelephonyManager; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 49 | import android.text.format.DateUtils; |
| 50 | import android.text.format.Formatter; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 51 | import android.text.format.Time; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 52 | import android.util.Log; |
| 53 | import android.view.LayoutInflater; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 54 | import android.view.Menu; |
| 55 | import android.view.MenuInflater; |
| 56 | import android.view.MenuItem; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 57 | import android.view.View; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 58 | import android.view.View.OnClickListener; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 59 | import android.view.ViewGroup; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 60 | import android.widget.AbsListView; |
| 61 | import android.widget.AdapterView; |
| 62 | import android.widget.AdapterView.OnItemClickListener; |
| 63 | import android.widget.AdapterView.OnItemSelectedListener; |
| 64 | import android.widget.ArrayAdapter; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 65 | import android.widget.BaseAdapter; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 66 | import android.widget.LinearLayout; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 67 | import android.widget.ListView; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 68 | import android.widget.NumberPicker; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 69 | import android.widget.Spinner; |
| 70 | import android.widget.TabHost; |
| 71 | import android.widget.TabHost.OnTabChangeListener; |
| 72 | import android.widget.TabHost.TabContentFactory; |
| 73 | import android.widget.TabHost.TabSpec; |
| 74 | import android.widget.TabWidget; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 75 | import android.widget.TextView; |
| 76 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 77 | import com.android.settings.net.NetworkPolicyModifier; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 78 | import com.android.settings.widget.DataUsageChartView; |
| 79 | import com.android.settings.widget.DataUsageChartView.DataUsageChartListener; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 80 | import com.google.android.collect.Lists; |
| 81 | |
| 82 | import java.util.ArrayList; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 83 | import java.util.Arrays; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 84 | import java.util.Collections; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 85 | import java.util.Locale; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 86 | |
| 87 | public class DataUsageSummary extends Fragment { |
| 88 | private static final String TAG = "DataUsage"; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 89 | private static final boolean LOGD = true; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 90 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 91 | private static final int TEMPLATE_INVALID = -1; |
| 92 | |
| 93 | private static final String TAB_3G = "3g"; |
| 94 | private static final String TAB_4G = "4g"; |
| 95 | private static final String TAB_MOBILE = "mobile"; |
| 96 | private static final String TAB_WIFI = "wifi"; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 97 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 98 | private static final String TAG_CONFIRM_LIMIT = "confirmLimit"; |
| 99 | private static final String TAG_CYCLE_EDITOR = "cycleEditor"; |
| 100 | |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 101 | private static final long KB_IN_BYTES = 1024; |
| 102 | private static final long MB_IN_BYTES = KB_IN_BYTES * 1024; |
| 103 | private static final long GB_IN_BYTES = MB_IN_BYTES * 1024; |
| 104 | |
| 105 | private INetworkStatsService mStatsService; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 106 | private INetworkPolicyManager mPolicyService; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 107 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 108 | private TabHost mTabHost; |
| 109 | private TabWidget mTabWidget; |
| 110 | private ListView mListView; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 111 | private DataUsageAdapter mAdapter; |
| 112 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 113 | private View mHeader; |
| 114 | private LinearLayout mSwitches; |
| 115 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 116 | private SwitchPreference mDataEnabled; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 117 | private CheckBoxPreference mDisableAtLimit; |
| 118 | private View mDataEnabledView; |
| 119 | private View mDisableAtLimitView; |
| 120 | |
| 121 | private DataUsageChartView mChart; |
| 122 | |
| 123 | private Spinner mCycleSpinner; |
| 124 | private CycleAdapter mCycleAdapter; |
| 125 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 126 | // TODO: persist show wifi flag |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 127 | private boolean mShowWifi = false; |
| 128 | |
| 129 | private int mTemplate = TEMPLATE_INVALID; |
| 130 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 131 | private NetworkPolicyModifier mPolicyModifier; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 132 | private NetworkStatsHistory mHistory; |
| 133 | |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 134 | @Override |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 135 | public void onCreate(Bundle savedInstanceState) { |
| 136 | super.onCreate(savedInstanceState); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 137 | |
| 138 | mStatsService = INetworkStatsService.Stub.asInterface( |
| 139 | ServiceManager.getService(Context.NETWORK_STATS_SERVICE)); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 140 | mPolicyService = INetworkPolicyManager.Stub.asInterface( |
| 141 | ServiceManager.getService(Context.NETWORK_POLICY_SERVICE)); |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 142 | |
| 143 | final Context context = getActivity(); |
| 144 | final String subscriberId = getActiveSubscriberId(context); |
| 145 | mPolicyModifier = new NetworkPolicyModifier(mPolicyService, subscriberId); |
Jeff Sharkey | 94a9095 | 2011-06-13 22:31:09 -0700 | [diff] [blame] | 146 | mPolicyModifier.read(); |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 147 | |
| 148 | setHasOptionsMenu(true); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 149 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 150 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 151 | @Override |
| 152 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 153 | Bundle savedInstanceState) { |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 154 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 155 | final Context context = inflater.getContext(); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 156 | final View view = inflater.inflate(R.layout.data_usage_summary, container, false); |
| 157 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 158 | mTabHost = (TabHost) view.findViewById(android.R.id.tabhost); |
| 159 | mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs); |
| 160 | mListView = (ListView) view.findViewById(android.R.id.list); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 161 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 162 | mTabHost.setup(); |
| 163 | mTabHost.setOnTabChangedListener(mTabListener); |
| 164 | |
| 165 | mHeader = inflater.inflate(R.layout.data_usage_header, mListView, false); |
| 166 | mListView.addHeaderView(mHeader, null, false); |
| 167 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 168 | mDataEnabled = new SwitchPreference(context); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 169 | mDisableAtLimit = new CheckBoxPreference(context); |
| 170 | |
| 171 | // kick refresh once to force-create views |
| 172 | refreshPreferenceViews(); |
| 173 | |
| 174 | // TODO: remove once thin preferences are supported (48dip) |
| 175 | mDataEnabledView.setLayoutParams(new LinearLayout.LayoutParams(MATCH_PARENT, 72)); |
| 176 | mDisableAtLimitView.setLayoutParams(new LinearLayout.LayoutParams(MATCH_PARENT, 72)); |
| 177 | |
| 178 | mDataEnabledView.setOnClickListener(mDataEnabledListener); |
| 179 | mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener); |
| 180 | |
| 181 | mSwitches = (LinearLayout) mHeader.findViewById(R.id.switches); |
| 182 | mSwitches.addView(mDataEnabledView); |
| 183 | mSwitches.addView(mDisableAtLimitView); |
| 184 | |
| 185 | mCycleSpinner = (Spinner) mHeader.findViewById(R.id.cycles); |
| 186 | mCycleAdapter = new CycleAdapter(context); |
| 187 | mCycleSpinner.setAdapter(mCycleAdapter); |
| 188 | mCycleSpinner.setOnItemSelectedListener(mCycleListener); |
| 189 | |
| 190 | mChart = new DataUsageChartView(context); |
| 191 | mChart.setListener(mChartListener); |
| 192 | mChart.setLayoutParams(new AbsListView.LayoutParams(MATCH_PARENT, 350)); |
| 193 | mListView.addHeaderView(mChart, null, false); |
| 194 | |
| 195 | mAdapter = new DataUsageAdapter(); |
| 196 | mListView.setOnItemClickListener(mListListener); |
| 197 | mListView.setAdapter(mAdapter); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 198 | |
| 199 | return view; |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public void onResume() { |
| 204 | super.onResume(); |
| 205 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 206 | // this kicks off chain reaction which creates tabs, binds the body to |
| 207 | // selected network, and binds chart, cycles and detail list. |
| 208 | updateTabs(); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 211 | @Override |
| 212 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 213 | inflater.inflate(R.menu.data_usage, menu); |
| 214 | } |
| 215 | |
| 216 | @Override |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 217 | public void onPrepareOptionsMenu(Menu menu) { |
| 218 | final MenuItem split4g = menu.findItem(R.id.action_split_4g); |
| 219 | split4g.setChecked(mPolicyModifier.isMobilePolicySplit()); |
| 220 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 221 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 222 | @Override |
| 223 | public boolean onOptionsItemSelected(MenuItem item) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 224 | switch (item.getItemId()) { |
| 225 | case R.id.action_split_4g: { |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 226 | final boolean mobileSplit = !item.isChecked(); |
| 227 | mPolicyModifier.setMobilePolicySplit(mobileSplit); |
| 228 | item.setChecked(mPolicyModifier.isMobilePolicySplit()); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 229 | updateTabs(); |
| 230 | return true; |
| 231 | } |
| 232 | case R.id.action_show_wifi: { |
| 233 | mShowWifi = !item.isChecked(); |
| 234 | item.setChecked(mShowWifi); |
| 235 | updateTabs(); |
| 236 | return true; |
| 237 | } |
| 238 | } |
| 239 | return false; |
| 240 | } |
| 241 | |
Jeff Sharkey | 94a9095 | 2011-06-13 22:31:09 -0700 | [diff] [blame] | 242 | @Override |
| 243 | public void onDestroyView() { |
| 244 | super.onDestroyView(); |
| 245 | |
| 246 | mDataEnabledView = null; |
| 247 | mDisableAtLimitView = null; |
| 248 | } |
| 249 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 250 | /** |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 251 | * Rebuild all tabs based on {@link NetworkPolicyModifier} and |
| 252 | * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects |
| 253 | * first tab, and kicks off a full rebind of body contents. |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 254 | */ |
| 255 | private void updateTabs() { |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 256 | final boolean mobileSplit = mPolicyModifier.isMobilePolicySplit(); |
| 257 | final boolean tabsVisible = mobileSplit || mShowWifi; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 258 | mTabWidget.setVisibility(tabsVisible ? View.VISIBLE : View.GONE); |
| 259 | mTabHost.clearAllTabs(); |
| 260 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 261 | if (mobileSplit) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 262 | mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g)); |
| 263 | mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g)); |
| 264 | } |
| 265 | |
| 266 | if (mShowWifi) { |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 267 | if (!mobileSplit) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 268 | mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile)); |
| 269 | } |
| 270 | mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi)); |
| 271 | } |
| 272 | |
| 273 | if (mTabWidget.getTabCount() > 0) { |
| 274 | // select first tab, which will kick off updateBody() |
| 275 | mTabHost.setCurrentTab(0); |
| 276 | } else { |
| 277 | // no tabs shown; update body manually |
| 278 | updateBody(); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 282 | /** |
| 283 | * Factory that provide empty {@link View} to make {@link TabHost} happy. |
| 284 | */ |
| 285 | private TabContentFactory mEmptyTabContent = new TabContentFactory() { |
| 286 | /** {@inheritDoc} */ |
| 287 | public View createTabContent(String tag) { |
| 288 | return new View(mTabHost.getContext()); |
| 289 | } |
| 290 | }; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 291 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 292 | /** |
| 293 | * Build {@link TabSpec} with thin indicator, and empty content. |
| 294 | */ |
| 295 | private TabSpec buildTabSpec(String tag, int titleRes) { |
| 296 | final LayoutInflater inflater = LayoutInflater.from(mTabWidget.getContext()); |
| 297 | final View indicator = inflater.inflate( |
| 298 | R.layout.tab_indicator_thin_holo, mTabWidget, false); |
| 299 | final TextView title = (TextView) indicator.findViewById(android.R.id.title); |
| 300 | title.setText(titleRes); |
| 301 | return mTabHost.newTabSpec(tag).setIndicator(indicator).setContent(mEmptyTabContent); |
| 302 | } |
| 303 | |
| 304 | private OnTabChangeListener mTabListener = new OnTabChangeListener() { |
| 305 | /** {@inheritDoc} */ |
| 306 | public void onTabChanged(String tabId) { |
| 307 | // user changed tab; update body |
| 308 | updateBody(); |
| 309 | } |
| 310 | }; |
| 311 | |
| 312 | /** |
| 313 | * Update body content based on current tab. Loads |
| 314 | * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and |
| 315 | * binds them to visible controls. |
| 316 | */ |
| 317 | private void updateBody() { |
| 318 | final String tabTag = mTabHost.getCurrentTabTag(); |
| 319 | final String currentTab = tabTag != null ? tabTag : TAB_MOBILE; |
| 320 | |
| 321 | if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab); |
| 322 | |
| 323 | if (TAB_WIFI.equals(currentTab)) { |
| 324 | // wifi doesn't have any controls |
| 325 | mDataEnabledView.setVisibility(View.GONE); |
| 326 | mDisableAtLimitView.setVisibility(View.GONE); |
| 327 | mTemplate = TEMPLATE_WIFI; |
| 328 | |
| 329 | } else { |
| 330 | // make sure we show for non-wifi |
| 331 | mDataEnabledView.setVisibility(View.VISIBLE); |
| 332 | mDisableAtLimitView.setVisibility(View.VISIBLE); |
| 333 | } |
| 334 | |
| 335 | if (TAB_MOBILE.equals(currentTab)) { |
| 336 | mDataEnabled.setTitle(R.string.data_usage_enable_mobile); |
| 337 | mDisableAtLimit.setTitle(R.string.data_usage_disable_mobile_limit); |
| 338 | mTemplate = TEMPLATE_MOBILE_ALL; |
| 339 | |
| 340 | } else if (TAB_3G.equals(currentTab)) { |
| 341 | mDataEnabled.setTitle(R.string.data_usage_enable_3g); |
| 342 | mDisableAtLimit.setTitle(R.string.data_usage_disable_3g_limit); |
| 343 | mTemplate = TEMPLATE_MOBILE_3G_LOWER; |
| 344 | |
| 345 | } else if (TAB_4G.equals(currentTab)) { |
| 346 | mDataEnabled.setTitle(R.string.data_usage_enable_4g); |
| 347 | mDisableAtLimit.setTitle(R.string.data_usage_disable_4g_limit); |
| 348 | mTemplate = TEMPLATE_MOBILE_4G; |
| 349 | |
| 350 | } |
| 351 | |
| 352 | // TODO: populate checkbox based on radio preferences |
| 353 | mDataEnabled.setChecked(true); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 354 | |
| 355 | try { |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 356 | // load stats for current template |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 357 | mHistory = mStatsService.getHistoryForNetwork(mTemplate); |
| 358 | } catch (RemoteException e) { |
| 359 | // since we can't do much without policy or history, and we don't |
| 360 | // want to leave with half-baked UI, we bail hard. |
| 361 | throw new RuntimeException("problem reading network policy or stats", e); |
| 362 | } |
| 363 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 364 | // bind chart to historical stats |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 365 | mChart.bindNetworkStats(mHistory); |
| 366 | |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 367 | updatePolicy(true); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 368 | |
| 369 | // force scroll to top of body |
| 370 | mListView.smoothScrollToPosition(0); |
| 371 | |
| 372 | // kick preference views so they rebind from changes above |
| 373 | refreshPreferenceViews(); |
| 374 | } |
| 375 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 376 | private void setPolicyCycleDay(int cycleDay) { |
| 377 | if (LOGD) Log.d(TAG, "setPolicyCycleDay()"); |
| 378 | mPolicyModifier.setPolicyCycleDay(mTemplate, cycleDay); |
| 379 | updatePolicy(true); |
| 380 | } |
| 381 | |
| 382 | private void setPolicyWarningBytes(long warningBytes) { |
| 383 | if (LOGD) Log.d(TAG, "setPolicyWarningBytes()"); |
| 384 | mPolicyModifier.setPolicyWarningBytes(mTemplate, warningBytes); |
| 385 | updatePolicy(false); |
| 386 | } |
| 387 | |
| 388 | private void setPolicyLimitBytes(long limitBytes) { |
| 389 | if (LOGD) Log.d(TAG, "setPolicyLimitBytes()"); |
| 390 | mPolicyModifier.setPolicyLimitBytes(mTemplate, limitBytes); |
| 391 | updatePolicy(false); |
| 392 | } |
| 393 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 394 | /** |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 395 | * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for |
| 396 | * current {@link #mTemplate}. |
| 397 | */ |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 398 | private void updatePolicy(boolean refreshCycle) { |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 399 | final NetworkPolicy policy = mPolicyModifier.getPolicy(mTemplate); |
| 400 | |
| 401 | // reflect policy limit in checkbox |
| 402 | mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED); |
| 403 | mChart.bindNetworkPolicy(policy); |
| 404 | |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 405 | if (refreshCycle) { |
| 406 | // generate cycle list based on policy and available history |
| 407 | updateCycleList(policy); |
| 408 | } |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 409 | |
| 410 | // kick preference views so they rebind from changes above |
| 411 | refreshPreferenceViews(); |
| 412 | } |
| 413 | |
| 414 | /** |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 415 | * Return full time bounds (earliest and latest time recorded) of the given |
| 416 | * {@link NetworkStatsHistory}. |
| 417 | */ |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 418 | public static long[] getHistoryBounds(NetworkStatsHistory history) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 419 | final long currentTime = System.currentTimeMillis(); |
| 420 | |
| 421 | long start = currentTime; |
| 422 | long end = currentTime; |
| 423 | if (history.bucketCount > 0) { |
| 424 | start = history.bucketStart[0]; |
| 425 | end = history.bucketStart[history.bucketCount - 1]; |
| 426 | } |
| 427 | |
| 428 | return new long[] { start, end }; |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay} |
| 433 | * and available {@link NetworkStatsHistory} data. Always selects the newest |
| 434 | * item, updating the inspection range on {@link #mChart}. |
| 435 | */ |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 436 | private void updateCycleList(NetworkPolicy policy) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 437 | mCycleAdapter.clear(); |
| 438 | |
| 439 | final Context context = mCycleSpinner.getContext(); |
| 440 | |
| 441 | final long[] bounds = getHistoryBounds(mHistory); |
| 442 | final long historyStart = bounds[0]; |
| 443 | final long historyEnd = bounds[1]; |
| 444 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 445 | if (policy != null) { |
| 446 | // find the next cycle boundary |
| 447 | long cycleEnd = computeNextCycleBoundary(historyEnd, policy); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 448 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 449 | int guardCount = 0; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 450 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 451 | // walk backwards, generating all valid cycle ranges |
| 452 | while (cycleEnd > historyStart) { |
| 453 | final long cycleStart = computeLastCycleBoundary(cycleEnd, policy); |
| 454 | Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs=" |
| 455 | + historyStart); |
| 456 | mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd)); |
| 457 | cycleEnd = cycleStart; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 458 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 459 | // TODO: remove this guard once we have better testing |
| 460 | if (guardCount++ > 50) { |
| 461 | Log.wtf(TAG, "stuck generating ranges for bounds=" + Arrays.toString(bounds) |
| 462 | + " and policy=" + policy); |
| 463 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 464 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 465 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 466 | // one last cycle entry to modify policy cycle day |
| 467 | mCycleAdapter.add(new CycleChangeItem(context)); |
| 468 | |
| 469 | } else { |
| 470 | // no valid cycle; show all data |
| 471 | // TODO: offer simple ranges like "last week" etc |
| 472 | mCycleAdapter.add(new CycleItem(context, historyStart, historyEnd)); |
| 473 | |
| 474 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 475 | |
| 476 | // force pick the current cycle (first item) |
| 477 | mCycleSpinner.setSelection(0); |
| 478 | mCycleListener.onItemSelected(mCycleSpinner, null, 0, 0); |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Force rebind of hijacked {@link Preference} views. |
| 483 | */ |
| 484 | private void refreshPreferenceViews() { |
| 485 | mDataEnabledView = mDataEnabled.getView(mDataEnabledView, mListView); |
| 486 | mDisableAtLimitView = mDisableAtLimit.getView(mDisableAtLimitView, mListView); |
| 487 | } |
| 488 | |
| 489 | private OnClickListener mDataEnabledListener = new OnClickListener() { |
| 490 | /** {@inheritDoc} */ |
| 491 | public void onClick(View v) { |
| 492 | mDataEnabled.setChecked(!mDataEnabled.isChecked()); |
| 493 | refreshPreferenceViews(); |
| 494 | |
| 495 | // TODO: wire up to telephony to enable/disable radios |
| 496 | } |
| 497 | }; |
| 498 | |
| 499 | private OnClickListener mDisableAtLimitListener = new OnClickListener() { |
| 500 | /** {@inheritDoc} */ |
| 501 | public void onClick(View v) { |
| 502 | final boolean disableAtLimit = !mDisableAtLimit.isChecked(); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 503 | if (disableAtLimit) { |
| 504 | // enabling limit; show confirmation dialog which eventually |
| 505 | // calls setPolicyLimitBytes() once user confirms. |
| 506 | ConfirmLimitFragment.show(DataUsageSummary.this); |
| 507 | } else { |
| 508 | setPolicyLimitBytes(LIMIT_DISABLED); |
| 509 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 510 | } |
| 511 | }; |
| 512 | |
| 513 | private OnItemClickListener mListListener = new OnItemClickListener() { |
| 514 | /** {@inheritDoc} */ |
| 515 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 516 | final AppUsageItem app = (AppUsageItem) parent.getItemAtPosition(position); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 517 | |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 518 | final Bundle args = new Bundle(); |
| 519 | args.putInt(Intent.EXTRA_UID, app.uid); |
| 520 | |
| 521 | final PreferenceActivity activity = (PreferenceActivity) getActivity(); |
| 522 | activity.startPreferencePanel(DataUsageAppDetail.class.getName(), args, |
| 523 | R.string.data_usage_summary_title, null, null, 0); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 524 | } |
| 525 | }; |
| 526 | |
| 527 | private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() { |
| 528 | /** {@inheritDoc} */ |
| 529 | public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
| 530 | final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position); |
| 531 | if (cycle instanceof CycleChangeItem) { |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 532 | // show cycle editor; will eventually call setPolicyCycleDay() |
| 533 | // when user finishes editing. |
| 534 | CycleEditorFragment.show(DataUsageSummary.this); |
| 535 | |
| 536 | // reset spinner to something other than "change cycle..." |
| 537 | mCycleSpinner.setSelection(0); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 538 | |
| 539 | } else { |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 540 | if (LOGD) Log.d(TAG, "showing cycle " + cycle); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 541 | |
| 542 | // update chart to show selected cycle, and update detail data |
| 543 | // to match updated sweep bounds. |
| 544 | final long[] bounds = getHistoryBounds(mHistory); |
| 545 | mChart.setVisibleRange(cycle.start, cycle.end, bounds[1]); |
| 546 | |
| 547 | updateDetailData(); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | /** {@inheritDoc} */ |
| 552 | public void onNothingSelected(AdapterView<?> parent) { |
| 553 | // ignored |
| 554 | } |
| 555 | }; |
| 556 | |
| 557 | /** |
| 558 | * Update {@link #mAdapter} with sorted list of applications data usage, |
| 559 | * based on current inspection from {@link #mChart}. |
| 560 | */ |
| 561 | private void updateDetailData() { |
| 562 | if (LOGD) Log.d(TAG, "updateDetailData()"); |
| 563 | |
| 564 | try { |
| 565 | final long[] range = mChart.getInspectRange(); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 566 | final NetworkStats stats = mStatsService.getSummaryForAllUid( |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 567 | range[0], range[1], mTemplate); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 568 | mAdapter.bindStats(stats); |
| 569 | } catch (RemoteException e) { |
| 570 | Log.w(TAG, "problem reading stats"); |
| 571 | } |
| 572 | } |
| 573 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 574 | private static String getActiveSubscriberId(Context context) { |
| 575 | final TelephonyManager telephony = (TelephonyManager) context.getSystemService( |
| 576 | Context.TELEPHONY_SERVICE); |
| 577 | return telephony.getSubscriberId(); |
| 578 | } |
| 579 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 580 | private DataUsageChartListener mChartListener = new DataUsageChartListener() { |
| 581 | /** {@inheritDoc} */ |
| 582 | public void onInspectRangeChanged() { |
| 583 | if (LOGD) Log.d(TAG, "onInspectRangeChanged()"); |
| 584 | updateDetailData(); |
| 585 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 586 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 587 | /** {@inheritDoc} */ |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 588 | public void onWarningChanged() { |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 589 | setPolicyWarningBytes(mChart.getWarningBytes()); |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 590 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 591 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 592 | /** {@inheritDoc} */ |
| 593 | public void onLimitChanged() { |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 594 | setPolicyLimitBytes(mChart.getLimitBytes()); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 595 | } |
| 596 | }; |
| 597 | |
| 598 | |
| 599 | /** |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 600 | * List item that reflects a specific data usage cycle. |
| 601 | */ |
| 602 | public static class CycleItem { |
| 603 | public CharSequence label; |
| 604 | public long start; |
| 605 | public long end; |
| 606 | |
| 607 | private static final StringBuilder sBuilder = new StringBuilder(50); |
| 608 | private static final java.util.Formatter sFormatter = new java.util.Formatter( |
| 609 | sBuilder, Locale.getDefault()); |
| 610 | |
| 611 | CycleItem(CharSequence label) { |
| 612 | this.label = label; |
| 613 | } |
| 614 | |
| 615 | public CycleItem(Context context, long start, long end) { |
| 616 | this.label = formatDateRangeUtc(context, start, end); |
| 617 | this.start = start; |
| 618 | this.end = end; |
| 619 | } |
| 620 | |
| 621 | private static String formatDateRangeUtc(Context context, long start, long end) { |
| 622 | synchronized (sBuilder) { |
| 623 | sBuilder.setLength(0); |
| 624 | return DateUtils.formatDateRange(context, sFormatter, start, end, |
| 625 | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_MONTH, |
| 626 | Time.TIMEZONE_UTC).toString(); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | @Override |
| 631 | public String toString() { |
| 632 | return label.toString(); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * Special-case data usage cycle that triggers dialog to change |
| 638 | * {@link NetworkPolicy#cycleDay}. |
| 639 | */ |
| 640 | public static class CycleChangeItem extends CycleItem { |
| 641 | public CycleChangeItem(Context context) { |
| 642 | super(context.getString(R.string.data_usage_change_cycle)); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | public static class CycleAdapter extends ArrayAdapter<CycleItem> { |
| 647 | public CycleAdapter(Context context) { |
| 648 | super(context, android.R.layout.simple_spinner_item); |
| 649 | setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
| 650 | } |
| 651 | } |
| 652 | |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 653 | private static class AppUsageItem implements Comparable<AppUsageItem> { |
| 654 | public int uid; |
| 655 | public long total; |
| 656 | |
| 657 | /** {@inheritDoc} */ |
| 658 | public int compareTo(AppUsageItem another) { |
| 659 | return Long.compare(another.total, total); |
| 660 | } |
| 661 | } |
| 662 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 663 | /** |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 664 | * Adapter of applications, sorted by total usage descending. |
| 665 | */ |
| 666 | public static class DataUsageAdapter extends BaseAdapter { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 667 | private ArrayList<AppUsageItem> mItems = Lists.newArrayList(); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 668 | |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 669 | public void bindStats(NetworkStats stats) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 670 | mItems.clear(); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 671 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 672 | for (int i = 0; i < stats.size; i++) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 673 | final AppUsageItem item = new AppUsageItem(); |
| 674 | item.uid = stats.uid[i]; |
| 675 | item.total = stats.rx[i] + stats.tx[i]; |
| 676 | mItems.add(item); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 679 | Collections.sort(mItems); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 680 | notifyDataSetChanged(); |
| 681 | } |
| 682 | |
| 683 | @Override |
| 684 | public int getCount() { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 685 | return mItems.size(); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | @Override |
| 689 | public Object getItem(int position) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 690 | return mItems.get(position); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | @Override |
| 694 | public long getItemId(int position) { |
| 695 | return position; |
| 696 | } |
| 697 | |
| 698 | @Override |
| 699 | public View getView(int position, View convertView, ViewGroup parent) { |
| 700 | if (convertView == null) { |
| 701 | convertView = LayoutInflater.from(parent.getContext()).inflate( |
| 702 | android.R.layout.simple_list_item_2, parent, false); |
| 703 | } |
| 704 | |
| 705 | final Context context = parent.getContext(); |
| 706 | final PackageManager pm = context.getPackageManager(); |
| 707 | |
| 708 | final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1); |
| 709 | final TextView text2 = (TextView) convertView.findViewById(android.R.id.text2); |
| 710 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 711 | final AppUsageItem item = mItems.get(position); |
| 712 | text1.setText(pm.getNameForUid(item.uid)); |
| 713 | text2.setText(Formatter.formatFileSize(context, item.total)); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 714 | |
| 715 | return convertView; |
| 716 | } |
| 717 | |
| 718 | } |
| 719 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 720 | /** |
| 721 | * Dialog to request user confirmation before setting |
| 722 | * {@link NetworkPolicy#limitBytes}. |
| 723 | */ |
| 724 | public static class ConfirmLimitFragment extends DialogFragment { |
| 725 | public static final String EXTRA_MESSAGE_ID = "messageId"; |
| 726 | public static final String EXTRA_LIMIT_BYTES = "limitBytes"; |
| 727 | |
| 728 | public static void show(DataUsageSummary parent) { |
| 729 | final Bundle args = new Bundle(); |
| 730 | |
| 731 | // TODO: customize default limits based on network template |
| 732 | switch (parent.mTemplate) { |
| 733 | case TEMPLATE_MOBILE_3G_LOWER: { |
| 734 | args.putInt(EXTRA_MESSAGE_ID, R.string.data_usage_limit_dialog_3g); |
| 735 | args.putLong(EXTRA_LIMIT_BYTES, 5 * GB_IN_BYTES); |
| 736 | break; |
| 737 | } |
| 738 | case TEMPLATE_MOBILE_4G: { |
| 739 | args.putInt(EXTRA_MESSAGE_ID, R.string.data_usage_limit_dialog_4g); |
| 740 | args.putLong(EXTRA_LIMIT_BYTES, 5 * GB_IN_BYTES); |
| 741 | break; |
| 742 | } |
| 743 | case TEMPLATE_MOBILE_ALL: { |
| 744 | args.putInt(EXTRA_MESSAGE_ID, R.string.data_usage_limit_dialog_mobile); |
| 745 | args.putLong(EXTRA_LIMIT_BYTES, 5 * GB_IN_BYTES); |
| 746 | break; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | final ConfirmLimitFragment dialog = new ConfirmLimitFragment(); |
| 751 | dialog.setArguments(args); |
| 752 | dialog.setTargetFragment(parent, 0); |
| 753 | dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT); |
| 754 | } |
| 755 | |
| 756 | @Override |
| 757 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 758 | final Context context = getActivity(); |
| 759 | |
| 760 | final int messageId = getArguments().getInt(EXTRA_MESSAGE_ID); |
| 761 | final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES); |
| 762 | |
| 763 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 764 | builder.setTitle(R.string.data_usage_limit_dialog_title); |
| 765 | builder.setMessage(messageId); |
| 766 | |
| 767 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
| 768 | public void onClick(DialogInterface dialog, int which) { |
| 769 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 770 | if (target != null) { |
| 771 | target.setPolicyLimitBytes(limitBytes); |
| 772 | } |
| 773 | } |
| 774 | }); |
| 775 | |
| 776 | return builder.create(); |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | /** |
| 781 | * Dialog to edit {@link NetworkPolicy#cycleDay}. |
| 782 | */ |
| 783 | public static class CycleEditorFragment extends DialogFragment { |
| 784 | public static final String EXTRA_CYCLE_DAY = "cycleDay"; |
| 785 | |
| 786 | public static void show(DataUsageSummary parent) { |
| 787 | final NetworkPolicy policy = parent.mPolicyModifier.getPolicy(parent.mTemplate); |
| 788 | final Bundle args = new Bundle(); |
| 789 | args.putInt(CycleEditorFragment.EXTRA_CYCLE_DAY, policy.cycleDay); |
| 790 | |
| 791 | final CycleEditorFragment dialog = new CycleEditorFragment(); |
| 792 | dialog.setArguments(args); |
| 793 | dialog.setTargetFragment(parent, 0); |
| 794 | dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR); |
| 795 | } |
| 796 | |
| 797 | @Override |
| 798 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 799 | final Context context = getActivity(); |
| 800 | |
| 801 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 802 | final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext()); |
| 803 | |
| 804 | final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false); |
| 805 | final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day); |
| 806 | |
| 807 | final int oldCycleDay = getArguments().getInt(EXTRA_CYCLE_DAY, 1); |
| 808 | |
| 809 | cycleDayPicker.setMinValue(1); |
| 810 | cycleDayPicker.setMaxValue(31); |
| 811 | cycleDayPicker.setValue(oldCycleDay); |
| 812 | cycleDayPicker.setWrapSelectorWheel(true); |
| 813 | |
| 814 | builder.setTitle(R.string.data_usage_cycle_editor_title); |
| 815 | builder.setView(view); |
| 816 | |
| 817 | builder.setPositiveButton(R.string.data_usage_cycle_editor_positive, |
| 818 | new DialogInterface.OnClickListener() { |
| 819 | public void onClick(DialogInterface dialog, int which) { |
| 820 | final int cycleDay = cycleDayPicker.getValue(); |
| 821 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 822 | if (target != null) { |
| 823 | target.setPolicyCycleDay(cycleDay); |
| 824 | } |
| 825 | } |
| 826 | }); |
| 827 | |
| 828 | return builder.create(); |
| 829 | } |
| 830 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 831 | |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 832 | } |