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 | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 19 | import static android.net.ConnectivityManager.TYPE_ETHERNET; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 20 | import static android.net.ConnectivityManager.TYPE_MOBILE; |
Jeff Sharkey | bdf98e8 | 2011-11-10 17:17:24 -0800 | [diff] [blame] | 21 | import static android.net.ConnectivityManager.TYPE_WIFI; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 22 | import static android.net.ConnectivityManager.TYPE_WIMAX; |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 23 | import static android.net.NetworkPolicy.LIMIT_DISABLED; |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 24 | import static android.net.NetworkPolicy.WARNING_DISABLED; |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 25 | import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 26 | import static android.net.NetworkPolicyManager.POLICY_NONE; |
| 27 | import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 28 | import static android.net.NetworkPolicyManager.computeLastCycleBoundary; |
| 29 | import static android.net.NetworkPolicyManager.computeNextCycleBoundary; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 30 | import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER; |
| 31 | import static android.net.NetworkTemplate.MATCH_MOBILE_4G; |
| 32 | import static android.net.NetworkTemplate.MATCH_MOBILE_ALL; |
| 33 | import static android.net.NetworkTemplate.MATCH_WIFI; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 34 | import static android.net.NetworkTemplate.buildTemplateEthernet; |
| 35 | import static android.net.NetworkTemplate.buildTemplateMobile3gLower; |
| 36 | import static android.net.NetworkTemplate.buildTemplateMobile4g; |
| 37 | import static android.net.NetworkTemplate.buildTemplateMobileAll; |
| 38 | import static android.net.NetworkTemplate.buildTemplateWifi; |
Jeff Sharkey | 77dae91 | 2012-02-03 14:50:33 -0800 | [diff] [blame] | 39 | import static android.net.TrafficStats.GB_IN_BYTES; |
| 40 | import static android.net.TrafficStats.MB_IN_BYTES; |
Jeff Sharkey | a83a24f | 2011-09-16 01:52:39 -0700 | [diff] [blame] | 41 | import static android.net.TrafficStats.UID_REMOVED; |
| 42 | import static android.net.TrafficStats.UID_TETHERING; |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 43 | import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH; |
| 44 | import static android.text.format.DateUtils.FORMAT_SHOW_DATE; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 45 | import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 46 | import static com.android.internal.util.Preconditions.checkNotNull; |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 47 | import static com.android.settings.Utils.prepareCustomPreferencesList; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 48 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 49 | import android.animation.LayoutTransition; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 50 | import android.app.AlertDialog; |
| 51 | import android.app.Dialog; |
| 52 | import android.app.DialogFragment; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 53 | import android.app.Fragment; |
Amith Yamasani | 5ba0a02 | 2011-11-07 12:29:00 -0800 | [diff] [blame] | 54 | import android.app.FragmentManager; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 55 | import android.app.FragmentTransaction; |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 56 | import android.app.LoaderManager.LoaderCallbacks; |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 57 | import android.content.ContentResolver; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 58 | import android.content.Context; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 59 | import android.content.DialogInterface; |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 60 | import android.content.Intent; |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 61 | import android.content.Loader; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 62 | import android.content.SharedPreferences; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 63 | import android.content.pm.PackageManager; |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 64 | import android.content.res.Resources; |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 65 | import android.graphics.Color; |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 66 | import android.graphics.drawable.ColorDrawable; |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 67 | import android.graphics.drawable.Drawable; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 68 | import android.net.ConnectivityManager; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 69 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 70 | import android.net.INetworkStatsService; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 71 | import android.net.NetworkPolicy; |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 72 | import android.net.NetworkPolicyManager; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 73 | import android.net.NetworkStats; |
| 74 | import android.net.NetworkStatsHistory; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 75 | import android.net.NetworkTemplate; |
Jeff Sharkey | aa5260e | 2011-06-14 23:21:59 -0700 | [diff] [blame] | 76 | import android.os.AsyncTask; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 77 | import android.os.Bundle; |
Jeff Sharkey | 1ae43f9 | 2011-08-03 17:16:09 -0700 | [diff] [blame] | 78 | import android.os.INetworkManagementService; |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 79 | import android.os.Parcel; |
| 80 | import android.os.Parcelable; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 81 | import android.os.RemoteException; |
| 82 | import android.os.ServiceManager; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 83 | import android.os.SystemProperties; |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 84 | import android.os.UserId; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 85 | import android.preference.Preference; |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 86 | import android.provider.Settings; |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 87 | import android.telephony.TelephonyManager; |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 88 | import android.text.TextUtils; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 89 | import android.text.format.DateUtils; |
| 90 | import android.text.format.Formatter; |
Jeff Sharkey | e5223a0 | 2012-03-09 17:11:14 -0800 | [diff] [blame] | 91 | import android.text.format.Time; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 92 | import android.util.Log; |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 93 | import android.util.SparseArray; |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 94 | import android.util.SparseBooleanArray; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 95 | import android.view.LayoutInflater; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 96 | import android.view.Menu; |
| 97 | import android.view.MenuInflater; |
| 98 | import android.view.MenuItem; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 99 | import android.view.View; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 100 | import android.view.View.OnClickListener; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 101 | import android.view.ViewGroup; |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 102 | import android.view.ViewTreeObserver.OnGlobalLayoutListener; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 103 | import android.widget.AdapterView; |
| 104 | import android.widget.AdapterView.OnItemClickListener; |
| 105 | import android.widget.AdapterView.OnItemSelectedListener; |
| 106 | import android.widget.ArrayAdapter; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 107 | import android.widget.BaseAdapter; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 108 | import android.widget.Button; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 109 | import android.widget.CheckBox; |
| 110 | import android.widget.CompoundButton; |
| 111 | import android.widget.CompoundButton.OnCheckedChangeListener; |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 112 | import android.widget.ImageView; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 113 | import android.widget.LinearLayout; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 114 | import android.widget.ListView; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 115 | import android.widget.NumberPicker; |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 116 | import android.widget.ProgressBar; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 117 | import android.widget.Spinner; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 118 | import android.widget.Switch; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 119 | import android.widget.TabHost; |
| 120 | import android.widget.TabHost.OnTabChangeListener; |
| 121 | import android.widget.TabHost.TabContentFactory; |
| 122 | import android.widget.TabHost.TabSpec; |
| 123 | import android.widget.TabWidget; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 124 | import android.widget.TextView; |
| 125 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 126 | import com.android.internal.telephony.Phone; |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 127 | import com.android.settings.drawable.InsetBoundsDrawable; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 128 | import com.android.settings.net.ChartData; |
| 129 | import com.android.settings.net.ChartDataLoader; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 130 | import com.android.settings.net.NetworkPolicyEditor; |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 131 | import com.android.settings.net.SummaryForAllUidLoader; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 132 | import com.android.settings.net.UidDetail; |
| 133 | import com.android.settings.net.UidDetailProvider; |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 134 | import com.android.settings.widget.ChartDataUsageView; |
| 135 | import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener; |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 136 | import com.android.settings.widget.PieChartView; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 137 | import com.google.android.collect.Lists; |
| 138 | |
| 139 | import java.util.ArrayList; |
| 140 | import java.util.Collections; |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 141 | import java.util.List; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 142 | import java.util.Locale; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 143 | |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 144 | import libcore.util.Objects; |
| 145 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 146 | /** |
| 147 | * Panel show data usage history across various networks, including options to |
| 148 | * inspect based on usage cycle and control through {@link NetworkPolicy}. |
| 149 | */ |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 150 | public class DataUsageSummary extends Fragment { |
| 151 | private static final String TAG = "DataUsage"; |
Jeff Sharkey | bdf98e8 | 2011-11-10 17:17:24 -0800 | [diff] [blame] | 152 | private static final boolean LOGD = false; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 153 | |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 154 | // TODO: remove this testing code |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 155 | private static final boolean TEST_ANIM = false; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 156 | private static final boolean TEST_RADIOS = false; |
Jeff Sharkey | f3871fb | 2012-02-03 19:27:07 -0800 | [diff] [blame] | 157 | |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 158 | private static final String TEST_RADIOS_PROP = "test.radios"; |
Jeff Sharkey | f3871fb | 2012-02-03 19:27:07 -0800 | [diff] [blame] | 159 | private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid"; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 160 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 161 | private static final String TAB_3G = "3g"; |
| 162 | private static final String TAB_4G = "4g"; |
| 163 | private static final String TAB_MOBILE = "mobile"; |
| 164 | private static final String TAB_WIFI = "wifi"; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 165 | private static final String TAB_ETHERNET = "ethernet"; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 166 | |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 167 | private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable"; |
| 168 | private static final String TAG_CONFIRM_DATA_ROAMING = "confirmDataRoaming"; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 169 | private static final String TAG_CONFIRM_LIMIT = "confirmLimit"; |
| 170 | private static final String TAG_CYCLE_EDITOR = "cycleEditor"; |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 171 | private static final String TAG_WARNING_EDITOR = "warningEditor"; |
| 172 | private static final String TAG_LIMIT_EDITOR = "limitEditor"; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 173 | private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict"; |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 174 | private static final String TAG_DENIED_RESTRICT = "deniedRestrict"; |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 175 | private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict"; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 176 | private static final String TAG_APP_DETAILS = "appDetails"; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 177 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 178 | private static final int LOADER_CHART_DATA = 2; |
| 179 | private static final int LOADER_SUMMARY = 3; |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 180 | |
Jeff Sharkey | 1ae43f9 | 2011-08-03 17:16:09 -0700 | [diff] [blame] | 181 | private INetworkManagementService mNetworkService; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 182 | private INetworkStatsService mStatsService; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 183 | private INetworkPolicyManager mPolicyService; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 184 | private ConnectivityManager mConnService; |
| 185 | |
| 186 | private static final String PREF_FILE = "data_usage"; |
| 187 | private static final String PREF_SHOW_WIFI = "show_wifi"; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 188 | private static final String PREF_SHOW_ETHERNET = "show_ethernet"; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 189 | |
| 190 | private SharedPreferences mPrefs; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 191 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 192 | private TabHost mTabHost; |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 193 | private ViewGroup mTabsContainer; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 194 | private TabWidget mTabWidget; |
| 195 | private ListView mListView; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 196 | private DataUsageAdapter mAdapter; |
| 197 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 198 | /** Distance to inset content from sides, when needed. */ |
| 199 | private int mInsetSide = 0; |
| 200 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 201 | private ViewGroup mHeader; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 202 | |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 203 | private ViewGroup mNetworkSwitchesContainer; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 204 | private LinearLayout mNetworkSwitches; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 205 | private Switch mDataEnabled; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 206 | private View mDataEnabledView; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 207 | private CheckBox mDisableAtLimit; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 208 | private View mDisableAtLimitView; |
| 209 | |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 210 | private View mCycleView; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 211 | private Spinner mCycleSpinner; |
| 212 | private CycleAdapter mCycleAdapter; |
| 213 | |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 214 | private ChartDataUsageView mChart; |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 215 | private TextView mUsageSummary; |
Jeff Sharkey | e2afc0f | 2011-08-01 15:29:30 -0700 | [diff] [blame] | 216 | private TextView mEmpty; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 217 | |
| 218 | private View mAppDetail; |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 219 | private ImageView mAppIcon; |
| 220 | private ViewGroup mAppTitles; |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 221 | private PieChartView mAppPieChart; |
| 222 | private TextView mAppForeground; |
| 223 | private TextView mAppBackground; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 224 | private Button mAppSettings; |
| 225 | |
| 226 | private LinearLayout mAppSwitches; |
| 227 | private CheckBox mAppRestrict; |
| 228 | private View mAppRestrictView; |
| 229 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 230 | private boolean mShowWifi = false; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 231 | private boolean mShowEthernet = false; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 232 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 233 | private NetworkTemplate mTemplate; |
| 234 | private ChartData mChartData; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 235 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 236 | private AppItem mCurrentApp = null; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 237 | |
| 238 | private Intent mAppSettingsIntent; |
| 239 | |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 240 | private NetworkPolicyEditor mPolicyEditor; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 241 | |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 242 | private String mCurrentTab = null; |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 243 | private String mIntentTab = null; |
| 244 | |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 245 | private MenuItem mMenuDataRoaming; |
| 246 | private MenuItem mMenuRestrictBackground; |
| 247 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 248 | /** Flag used to ignore listeners during binding. */ |
| 249 | private boolean mBinding; |
| 250 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 251 | private UidDetailProvider mUidDetailProvider; |
| 252 | |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 253 | @Override |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 254 | public void onCreate(Bundle savedInstanceState) { |
| 255 | super.onCreate(savedInstanceState); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 256 | |
Jeff Sharkey | 1ae43f9 | 2011-08-03 17:16:09 -0700 | [diff] [blame] | 257 | mNetworkService = INetworkManagementService.Stub.asInterface( |
| 258 | ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE)); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 259 | mStatsService = INetworkStatsService.Stub.asInterface( |
| 260 | ServiceManager.getService(Context.NETWORK_STATS_SERVICE)); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 261 | mPolicyService = INetworkPolicyManager.Stub.asInterface( |
| 262 | ServiceManager.getService(Context.NETWORK_POLICY_SERVICE)); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 263 | mConnService = (ConnectivityManager) getActivity().getSystemService( |
| 264 | Context.CONNECTIVITY_SERVICE); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 265 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 266 | mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE); |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 267 | |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 268 | mPolicyEditor = new NetworkPolicyEditor(mPolicyService); |
| 269 | mPolicyEditor.read(); |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 270 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 271 | mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 272 | mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 273 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 274 | setHasOptionsMenu(true); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 275 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 276 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 277 | @Override |
| 278 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 279 | Bundle savedInstanceState) { |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 280 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 281 | final Context context = inflater.getContext(); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 282 | final View view = inflater.inflate(R.layout.data_usage_summary, container, false); |
| 283 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 284 | mUidDetailProvider = new UidDetailProvider(context); |
| 285 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 286 | mTabHost = (TabHost) view.findViewById(android.R.id.tabhost); |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 287 | mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 288 | mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs); |
| 289 | mListView = (ListView) view.findViewById(android.R.id.list); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 290 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 291 | // decide if we need to manually inset our content, or if we should rely |
| 292 | // on parent container for inset. |
| 293 | final boolean shouldInset = mListView.getScrollBarStyle() |
| 294 | == View.SCROLLBARS_OUTSIDE_OVERLAY; |
| 295 | if (shouldInset) { |
| 296 | mInsetSide = view.getResources().getDimensionPixelOffset( |
| 297 | com.android.internal.R.dimen.preference_fragment_padding_side); |
| 298 | } else { |
| 299 | mInsetSide = 0; |
| 300 | } |
| 301 | |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 302 | // adjust padding around tabwidget as needed |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 303 | prepareCustomPreferencesList(container, view, mListView, true); |
| 304 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 305 | mTabHost.setup(); |
| 306 | mTabHost.setOnTabChangedListener(mTabListener); |
| 307 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 308 | mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 309 | mHeader.setClickable(true); |
| 310 | |
| 311 | mListView.addHeaderView(mHeader, null, true); |
| 312 | mListView.setItemsCanFocus(true); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 313 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 314 | if (mInsetSide > 0) { |
| 315 | // inset selector and divider drawables |
| 316 | insetListViewDrawables(mListView, mInsetSide); |
| 317 | mHeader.setPadding(mInsetSide, 0, mInsetSide, 0); |
| 318 | } |
| 319 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 320 | { |
| 321 | // bind network switches |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 322 | mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById( |
| 323 | R.id.network_switches_container); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 324 | mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 325 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 326 | mDataEnabled = new Switch(inflater.getContext()); |
| 327 | mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled); |
| 328 | mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener); |
| 329 | mNetworkSwitches.addView(mDataEnabledView); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 330 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 331 | mDisableAtLimit = new CheckBox(inflater.getContext()); |
| 332 | mDisableAtLimit.setClickable(false); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 333 | mDisableAtLimit.setFocusable(false); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 334 | mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 335 | mDisableAtLimitView.setClickable(true); |
| 336 | mDisableAtLimitView.setFocusable(true); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 337 | mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener); |
| 338 | mNetworkSwitches.addView(mDisableAtLimitView); |
| 339 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 340 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 341 | // bind cycle dropdown |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 342 | mCycleView = mHeader.findViewById(R.id.cycles); |
| 343 | mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 344 | mCycleAdapter = new CycleAdapter(context); |
| 345 | mCycleSpinner.setAdapter(mCycleAdapter); |
| 346 | mCycleSpinner.setOnItemSelectedListener(mCycleListener); |
| 347 | |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 348 | mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 349 | mChart.setListener(mChartListener); |
Jeff Sharkey | bdf98e8 | 2011-11-10 17:17:24 -0800 | [diff] [blame] | 350 | mChart.bindNetworkPolicy(null); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 351 | |
| 352 | { |
| 353 | // bind app detail controls |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 354 | mAppDetail = mHeader.findViewById(R.id.app_detail); |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 355 | mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon); |
| 356 | mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles); |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 357 | mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart); |
| 358 | mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground); |
| 359 | mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background); |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 360 | mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 361 | |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 362 | mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 363 | mAppSettings.setOnClickListener(mAppSettingsListener); |
| 364 | |
| 365 | mAppRestrict = new CheckBox(inflater.getContext()); |
| 366 | mAppRestrict.setClickable(false); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 367 | mAppRestrict.setFocusable(false); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 368 | mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 369 | mAppRestrictView.setClickable(true); |
| 370 | mAppRestrictView.setFocusable(true); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 371 | mAppRestrictView.setOnClickListener(mAppRestrictListener); |
| 372 | mAppSwitches.addView(mAppRestrictView); |
| 373 | } |
| 374 | |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 375 | mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary); |
Jeff Sharkey | e2afc0f | 2011-08-01 15:29:30 -0700 | [diff] [blame] | 376 | mEmpty = (TextView) mHeader.findViewById(android.R.id.empty); |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 377 | |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 378 | // only assign layout transitions once first layout is finished |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 379 | mListView.getViewTreeObserver().addOnGlobalLayoutListener(mFirstLayoutListener); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 380 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 381 | mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 382 | mListView.setOnItemClickListener(mListListener); |
| 383 | mListView.setAdapter(mAdapter); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 384 | |
| 385 | return view; |
| 386 | } |
| 387 | |
| 388 | @Override |
| 389 | public void onResume() { |
| 390 | super.onResume(); |
| 391 | |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 392 | // pick default tab based on incoming intent |
| 393 | final Intent intent = getActivity().getIntent(); |
| 394 | mIntentTab = computeTabFromIntent(intent); |
| 395 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 396 | // this kicks off chain reaction which creates tabs, binds the body to |
| 397 | // selected network, and binds chart, cycles and detail list. |
| 398 | updateTabs(); |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 399 | |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 400 | // kick off background task to update stats |
| 401 | new AsyncTask<Void, Void, Void>() { |
| 402 | @Override |
| 403 | protected Void doInBackground(Void... params) { |
| 404 | try { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 405 | // wait a few seconds before kicking off |
| 406 | Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS); |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 407 | mStatsService.forceUpdate(); |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 408 | } catch (InterruptedException e) { |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 409 | } catch (RemoteException e) { |
| 410 | } |
| 411 | return null; |
| 412 | } |
| 413 | |
| 414 | @Override |
| 415 | protected void onPostExecute(Void result) { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 416 | if (isAdded()) { |
| 417 | updateBody(); |
| 418 | } |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 419 | } |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 420 | }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 423 | @Override |
| 424 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 425 | inflater.inflate(R.menu.data_usage, menu); |
| 426 | } |
| 427 | |
| 428 | @Override |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 429 | public void onPrepareOptionsMenu(Menu menu) { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 430 | final Context context = getActivity(); |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 431 | final boolean appDetailMode = isAppDetailMode(); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 432 | |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 433 | mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming); |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 434 | mMenuDataRoaming.setVisible(hasMobileRadio(context) && !appDetailMode); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 435 | mMenuDataRoaming.setChecked(getDataRoaming()); |
| 436 | |
| 437 | mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background); |
Jeff Sharkey | bdf98e8 | 2011-11-10 17:17:24 -0800 | [diff] [blame] | 438 | mMenuRestrictBackground.setVisible(hasMobileRadio(context) && !appDetailMode); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 439 | mMenuRestrictBackground.setChecked(getRestrictBackground()); |
| 440 | |
| 441 | final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g); |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 442 | split4g.setVisible(hasMobile4gRadio(context) && !appDetailMode); |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 443 | split4g.setChecked(isMobilePolicySplit()); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 444 | |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 445 | final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 446 | if (hasWifiRadio(context) && hasMobileRadio(context)) { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 447 | showWifi.setVisible(!appDetailMode); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 448 | showWifi.setChecked(mShowWifi); |
| 449 | } else { |
| 450 | showWifi.setVisible(false); |
| 451 | mShowWifi = true; |
| 452 | } |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 453 | |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 454 | final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet); |
| 455 | if (hasEthernet(context) && hasMobileRadio(context)) { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 456 | showEthernet.setVisible(!appDetailMode); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 457 | showEthernet.setChecked(mShowEthernet); |
| 458 | } else { |
| 459 | showEthernet.setVisible(false); |
| 460 | mShowEthernet = true; |
| 461 | } |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 462 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 463 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 464 | @Override |
| 465 | public boolean onOptionsItemSelected(MenuItem item) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 466 | switch (item.getItemId()) { |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 467 | case R.id.data_usage_menu_roaming: { |
| 468 | final boolean dataRoaming = !item.isChecked(); |
| 469 | if (dataRoaming) { |
| 470 | ConfirmDataRoamingFragment.show(this); |
| 471 | } else { |
| 472 | // no confirmation to disable roaming |
| 473 | setDataRoaming(false); |
| 474 | } |
| 475 | return true; |
| 476 | } |
| 477 | case R.id.data_usage_menu_restrict_background: { |
| 478 | final boolean restrictBackground = !item.isChecked(); |
| 479 | if (restrictBackground) { |
Jeff Sharkey | 3038c52 | 2011-11-30 15:37:51 -0800 | [diff] [blame] | 480 | ConfirmRestrictFragment.show(this); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 481 | } else { |
| 482 | // no confirmation to drop restriction |
| 483 | setRestrictBackground(false); |
| 484 | } |
| 485 | return true; |
| 486 | } |
| 487 | case R.id.data_usage_menu_split_4g: { |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 488 | final boolean mobileSplit = !item.isChecked(); |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 489 | setMobilePolicySplit(mobileSplit); |
| 490 | item.setChecked(isMobilePolicySplit()); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 491 | updateTabs(); |
| 492 | return true; |
| 493 | } |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 494 | case R.id.data_usage_menu_show_wifi: { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 495 | mShowWifi = !item.isChecked(); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 496 | mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply(); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 497 | item.setChecked(mShowWifi); |
| 498 | updateTabs(); |
| 499 | return true; |
| 500 | } |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 501 | case R.id.data_usage_menu_show_ethernet: { |
| 502 | mShowEthernet = !item.isChecked(); |
| 503 | mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply(); |
| 504 | item.setChecked(mShowEthernet); |
| 505 | updateTabs(); |
| 506 | return true; |
| 507 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 508 | } |
| 509 | return false; |
| 510 | } |
| 511 | |
Jeff Sharkey | 94a9095 | 2011-06-13 22:31:09 -0700 | [diff] [blame] | 512 | @Override |
| 513 | public void onDestroyView() { |
| 514 | super.onDestroyView(); |
| 515 | |
| 516 | mDataEnabledView = null; |
| 517 | mDisableAtLimitView = null; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 518 | |
| 519 | mUidDetailProvider.clearCache(); |
| 520 | mUidDetailProvider = null; |
Jeff Sharkey | 94a9095 | 2011-06-13 22:31:09 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Amith Yamasani | 5ba0a02 | 2011-11-07 12:29:00 -0800 | [diff] [blame] | 523 | @Override |
| 524 | public void onDestroy() { |
| 525 | if (this.isRemoving()) { |
| 526 | getFragmentManager() |
| 527 | .popBackStack(TAG_APP_DETAILS, FragmentManager.POP_BACK_STACK_INCLUSIVE); |
| 528 | } |
| 529 | super.onDestroy(); |
| 530 | } |
| 531 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 532 | /** |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 533 | * Listener to setup {@link LayoutTransition} after first layout pass. |
| 534 | */ |
| 535 | private OnGlobalLayoutListener mFirstLayoutListener = new OnGlobalLayoutListener() { |
| 536 | /** {@inheritDoc} */ |
| 537 | public void onGlobalLayout() { |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 538 | mListView.getViewTreeObserver().removeGlobalOnLayoutListener(mFirstLayoutListener); |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 539 | |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 540 | mTabsContainer.setLayoutTransition(buildLayoutTransition()); |
| 541 | mHeader.setLayoutTransition(buildLayoutTransition()); |
| 542 | mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition()); |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 543 | |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 544 | final LayoutTransition chartTransition = buildLayoutTransition(); |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 545 | chartTransition.setStartDelay(LayoutTransition.APPEARING, 0); |
| 546 | chartTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0); |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 547 | chartTransition.setAnimator(LayoutTransition.APPEARING, null); |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 548 | chartTransition.setAnimator(LayoutTransition.DISAPPEARING, null); |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 549 | mChart.setLayoutTransition(chartTransition); |
| 550 | } |
| 551 | }; |
| 552 | |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 553 | private static LayoutTransition buildLayoutTransition() { |
| 554 | final LayoutTransition transition = new LayoutTransition(); |
| 555 | if (TEST_ANIM) { |
| 556 | transition.setDuration(1500); |
| 557 | } |
| 558 | transition.setAnimateParentHierarchy(false); |
| 559 | return transition; |
| 560 | } |
| 561 | |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 562 | /** |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 563 | * Rebuild all tabs based on {@link NetworkPolicyEditor} and |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 564 | * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects |
| 565 | * first tab, and kicks off a full rebind of body contents. |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 566 | */ |
| 567 | private void updateTabs() { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 568 | final Context context = getActivity(); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 569 | mTabHost.clearAllTabs(); |
| 570 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 571 | final boolean mobileSplit = isMobilePolicySplit(); |
| 572 | if (mobileSplit && hasMobile4gRadio(context)) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 573 | mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g)); |
| 574 | mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g)); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 575 | } else if (hasMobileRadio(context)) { |
| 576 | mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile)); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 577 | } |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 578 | if (mShowWifi && hasWifiRadio(context)) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 579 | mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi)); |
| 580 | } |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 581 | if (mShowEthernet && hasEthernet(context)) { |
| 582 | mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet)); |
| 583 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 584 | |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 585 | final boolean multipleTabs = mTabWidget.getTabCount() > 1; |
| 586 | mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE); |
| 587 | if (mIntentTab != null) { |
| 588 | if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 589 | // already hit updateBody() when added; ignore |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 590 | updateBody(); |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 591 | } else { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 592 | mTabHost.setCurrentTabByTag(mIntentTab); |
| 593 | } |
| 594 | mIntentTab = null; |
| 595 | } else { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 596 | // already hit updateBody() when added; ignore |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 600 | /** |
| 601 | * Factory that provide empty {@link View} to make {@link TabHost} happy. |
| 602 | */ |
| 603 | private TabContentFactory mEmptyTabContent = new TabContentFactory() { |
| 604 | /** {@inheritDoc} */ |
| 605 | public View createTabContent(String tag) { |
| 606 | return new View(mTabHost.getContext()); |
| 607 | } |
| 608 | }; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 609 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 610 | /** |
| 611 | * Build {@link TabSpec} with thin indicator, and empty content. |
| 612 | */ |
| 613 | private TabSpec buildTabSpec(String tag, int titleRes) { |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 614 | return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent( |
| 615 | mEmptyTabContent); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | private OnTabChangeListener mTabListener = new OnTabChangeListener() { |
| 619 | /** {@inheritDoc} */ |
| 620 | public void onTabChanged(String tabId) { |
| 621 | // user changed tab; update body |
| 622 | updateBody(); |
| 623 | } |
| 624 | }; |
| 625 | |
| 626 | /** |
| 627 | * Update body content based on current tab. Loads |
| 628 | * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and |
| 629 | * binds them to visible controls. |
| 630 | */ |
| 631 | private void updateBody() { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 632 | mBinding = true; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 633 | if (!isAdded()) return; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 634 | |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 635 | final Context context = getActivity(); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 636 | final String currentTab = mTabHost.getCurrentTabTag(); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 637 | |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 638 | if (currentTab == null) { |
| 639 | Log.w(TAG, "no tab selected; hiding body"); |
| 640 | mListView.setVisibility(View.GONE); |
| 641 | return; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 642 | } else { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 643 | mListView.setVisibility(View.VISIBLE); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 644 | } |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 645 | |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 646 | final boolean tabChanged = !currentTab.equals(mCurrentTab); |
| 647 | mCurrentTab = currentTab; |
| 648 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 649 | if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab); |
| 650 | |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 651 | mDataEnabledView.setVisibility(View.VISIBLE); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 652 | |
| 653 | if (TAB_MOBILE.equals(currentTab)) { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 654 | setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile); |
| 655 | setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 656 | mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context)); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 657 | |
| 658 | } else if (TAB_3G.equals(currentTab)) { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 659 | setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g); |
| 660 | setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit); |
| 661 | // TODO: bind mDataEnabled to 3G radio state |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 662 | mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context)); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 663 | |
| 664 | } else if (TAB_4G.equals(currentTab)) { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 665 | setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g); |
| 666 | setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit); |
| 667 | // TODO: bind mDataEnabled to 4G radio state |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 668 | mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context)); |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 669 | |
| 670 | } else if (TAB_WIFI.equals(currentTab)) { |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 671 | // wifi doesn't have any controls |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 672 | mDataEnabledView.setVisibility(View.GONE); |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 673 | mDisableAtLimitView.setVisibility(View.GONE); |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 674 | mTemplate = buildTemplateWifi(); |
| 675 | |
| 676 | } else if (TAB_ETHERNET.equals(currentTab)) { |
| 677 | // ethernet doesn't have any controls |
| 678 | mDataEnabledView.setVisibility(View.GONE); |
| 679 | mDisableAtLimitView.setVisibility(View.GONE); |
| 680 | mTemplate = buildTemplateEthernet(); |
| 681 | |
| 682 | } else { |
| 683 | throw new IllegalStateException("unknown tab: " + currentTab); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 686 | // kick off loader for network history |
| 687 | // TODO: consider chaining two loaders together instead of reloading |
| 688 | // network history when showing app detail. |
| 689 | getLoaderManager().restartLoader(LOADER_CHART_DATA, |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 690 | ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 691 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 692 | // detail mode can change visible menus, invalidate |
| 693 | getActivity().invalidateOptionsMenu(); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 694 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 695 | mBinding = false; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 698 | private boolean isAppDetailMode() { |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 699 | return mCurrentApp != null; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | /** |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 703 | * Update UID details panels to match {@link #mCurrentApp}, showing or |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 704 | * hiding them depending on {@link #isAppDetailMode()}. |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 705 | */ |
| 706 | private void updateAppDetail() { |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 707 | final Context context = getActivity(); |
| 708 | final PackageManager pm = context.getPackageManager(); |
| 709 | final LayoutInflater inflater = getActivity().getLayoutInflater(); |
| 710 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 711 | if (isAppDetailMode()) { |
| 712 | mAppDetail.setVisibility(View.VISIBLE); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 713 | mCycleAdapter.setChangeVisible(false); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 714 | } else { |
| 715 | mAppDetail.setVisibility(View.GONE); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 716 | mCycleAdapter.setChangeVisible(true); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 717 | |
| 718 | // hide detail stats when not in detail mode |
| 719 | mChart.bindDetailNetworkStats(null); |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | // remove warning/limit sweeps while in detail mode |
| 724 | mChart.bindNetworkPolicy(null); |
| 725 | |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 726 | // show icon and all labels appearing under this app |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 727 | final int appId = mCurrentApp.appId; |
| 728 | final UidDetail detail = mUidDetailProvider.getUidDetail(appId, true); |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 729 | mAppIcon.setImageDrawable(detail.icon); |
| 730 | |
| 731 | mAppTitles.removeAllViews(); |
| 732 | if (detail.detailLabels != null) { |
| 733 | for (CharSequence label : detail.detailLabels) { |
| 734 | mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label)); |
| 735 | } |
| 736 | } else { |
| 737 | mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label)); |
| 738 | } |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 739 | |
| 740 | // enable settings button when package provides it |
| 741 | // TODO: target torwards entire UID instead of just first package |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 742 | final String[] packageNames = pm.getPackagesForUid(appId); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 743 | if (packageNames != null && packageNames.length > 0) { |
| 744 | mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE); |
| 745 | mAppSettingsIntent.setPackage(packageNames[0]); |
| 746 | mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT); |
| 747 | |
| 748 | final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null; |
| 749 | mAppSettings.setEnabled(matchFound); |
| 750 | |
| 751 | } else { |
| 752 | mAppSettingsIntent = null; |
| 753 | mAppSettings.setEnabled(false); |
| 754 | } |
| 755 | |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 756 | updateDetailData(); |
| 757 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 758 | if (UserId.isApp(appId) && !getRestrictBackground() && isBandwidthControlEnabled() |
Jeff Sharkey | bdf98e8 | 2011-11-10 17:17:24 -0800 | [diff] [blame] | 759 | && hasMobileRadio(context)) { |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 760 | setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background); |
Jeff Sharkey | 3038c52 | 2011-11-30 15:37:51 -0800 | [diff] [blame] | 761 | setPreferenceSummary(mAppRestrictView, |
| 762 | getString(R.string.data_usage_app_restrict_background_summary)); |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 763 | |
| 764 | mAppRestrictView.setVisibility(View.VISIBLE); |
| 765 | mAppRestrict.setChecked(getAppRestrictBackground()); |
| 766 | |
| 767 | } else { |
| 768 | mAppRestrictView.setVisibility(View.GONE); |
| 769 | } |
| 770 | } |
| 771 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 772 | private void setPolicyWarningBytes(long warningBytes) { |
| 773 | if (LOGD) Log.d(TAG, "setPolicyWarningBytes()"); |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 774 | mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 775 | updatePolicy(false); |
| 776 | } |
| 777 | |
| 778 | private void setPolicyLimitBytes(long limitBytes) { |
| 779 | if (LOGD) Log.d(TAG, "setPolicyLimitBytes()"); |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 780 | mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 781 | updatePolicy(false); |
| 782 | } |
| 783 | |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 784 | /** |
| 785 | * Local cache of value, used to work around delay when |
| 786 | * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async. |
| 787 | */ |
| 788 | private Boolean mMobileDataEnabled; |
| 789 | |
| 790 | private boolean isMobileDataEnabled() { |
| 791 | if (mMobileDataEnabled != null) { |
| 792 | // TODO: deprecate and remove this once enabled flag is on policy |
| 793 | return mMobileDataEnabled; |
| 794 | } else { |
| 795 | return mConnService.getMobileDataEnabled(); |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | private void setMobileDataEnabled(boolean enabled) { |
| 800 | if (LOGD) Log.d(TAG, "setMobileDataEnabled()"); |
| 801 | mConnService.setMobileDataEnabled(enabled); |
| 802 | mMobileDataEnabled = enabled; |
| 803 | updatePolicy(false); |
| 804 | } |
| 805 | |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 806 | private boolean isNetworkPolicyModifiable(NetworkPolicy policy) { |
| 807 | return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked(); |
Jeff Sharkey | 1ae43f9 | 2011-08-03 17:16:09 -0700 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | private boolean isBandwidthControlEnabled() { |
| 811 | try { |
| 812 | return mNetworkService.isBandwidthControlEnabled(); |
| 813 | } catch (RemoteException e) { |
| 814 | Log.w(TAG, "problem talking with INetworkManagementService: " + e); |
| 815 | return false; |
| 816 | } |
| 817 | } |
| 818 | |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 819 | private boolean getDataRoaming() { |
| 820 | final ContentResolver resolver = getActivity().getContentResolver(); |
| 821 | return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0; |
| 822 | } |
| 823 | |
| 824 | private void setDataRoaming(boolean enabled) { |
| 825 | // TODO: teach telephony DataConnectionTracker to watch and apply |
| 826 | // updates when changed. |
| 827 | final ContentResolver resolver = getActivity().getContentResolver(); |
| 828 | Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0); |
| 829 | mMenuDataRoaming.setChecked(enabled); |
| 830 | } |
| 831 | |
| 832 | private boolean getRestrictBackground() { |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 833 | try { |
| 834 | return mPolicyService.getRestrictBackground(); |
| 835 | } catch (RemoteException e) { |
| 836 | Log.w(TAG, "problem talking with policy service: " + e); |
| 837 | return false; |
| 838 | } |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | private void setRestrictBackground(boolean restrictBackground) { |
| 842 | if (LOGD) Log.d(TAG, "setRestrictBackground()"); |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 843 | try { |
| 844 | mPolicyService.setRestrictBackground(restrictBackground); |
| 845 | mMenuRestrictBackground.setChecked(restrictBackground); |
| 846 | } catch (RemoteException e) { |
| 847 | Log.w(TAG, "problem talking with policy service: " + e); |
| 848 | } |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | private boolean getAppRestrictBackground() { |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 852 | final int appId = mCurrentApp.appId; |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 853 | final int uidPolicy; |
| 854 | try { |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 855 | uidPolicy = mPolicyService.getAppPolicy(appId); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 856 | } catch (RemoteException e) { |
| 857 | // since we can't do much without policy, we bail hard. |
| 858 | throw new RuntimeException("problem reading network policy", e); |
| 859 | } |
| 860 | |
| 861 | return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0; |
| 862 | } |
| 863 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 864 | private void setAppRestrictBackground(boolean restrictBackground) { |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 865 | if (LOGD) Log.d(TAG, "setAppRestrictBackground()"); |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 866 | final int appId = mCurrentApp.appId; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 867 | try { |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 868 | mPolicyService.setAppPolicy(appId, |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 869 | restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 870 | } catch (RemoteException e) { |
| 871 | throw new RuntimeException("unable to save policy", e); |
| 872 | } |
| 873 | |
| 874 | mAppRestrict.setChecked(restrictBackground); |
| 875 | } |
| 876 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 877 | /** |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 878 | * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for |
| 879 | * current {@link #mTemplate}. |
| 880 | */ |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 881 | private void updatePolicy(boolean refreshCycle) { |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 882 | if (isAppDetailMode()) { |
| 883 | mNetworkSwitches.setVisibility(View.GONE); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 884 | } else { |
| 885 | mNetworkSwitches.setVisibility(View.VISIBLE); |
| 886 | } |
| 887 | |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 888 | // TODO: move enabled state directly into policy |
| 889 | if (TAB_MOBILE.equals(mCurrentTab)) { |
| 890 | mBinding = true; |
| 891 | mDataEnabled.setChecked(isMobileDataEnabled()); |
| 892 | mBinding = false; |
| 893 | } |
| 894 | |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 895 | final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate); |
| 896 | if (isNetworkPolicyModifiable(policy)) { |
Jeff Sharkey | 1ae43f9 | 2011-08-03 17:16:09 -0700 | [diff] [blame] | 897 | mDisableAtLimitView.setVisibility(View.VISIBLE); |
| 898 | mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED); |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 899 | if (!isAppDetailMode()) { |
| 900 | mChart.bindNetworkPolicy(policy); |
| 901 | } |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 902 | |
Jeff Sharkey | 1ae43f9 | 2011-08-03 17:16:09 -0700 | [diff] [blame] | 903 | } else { |
| 904 | // controls are disabled; don't bind warning/limit sweeps |
| 905 | mDisableAtLimitView.setVisibility(View.GONE); |
| 906 | mChart.bindNetworkPolicy(null); |
| 907 | } |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 908 | |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 909 | if (refreshCycle) { |
| 910 | // generate cycle list based on policy and available history |
| 911 | updateCycleList(policy); |
| 912 | } |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | /** |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 916 | * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay} |
| 917 | * and available {@link NetworkStatsHistory} data. Always selects the newest |
| 918 | * item, updating the inspection range on {@link #mChart}. |
| 919 | */ |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 920 | private void updateCycleList(NetworkPolicy policy) { |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 921 | // stash away currently selected cycle to try restoring below |
| 922 | final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem(); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 923 | mCycleAdapter.clear(); |
| 924 | |
| 925 | final Context context = mCycleSpinner.getContext(); |
| 926 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 927 | long historyStart = Long.MAX_VALUE; |
| 928 | long historyEnd = Long.MIN_VALUE; |
| 929 | if (mChartData != null) { |
| 930 | historyStart = mChartData.network.getStart(); |
| 931 | historyEnd = mChartData.network.getEnd(); |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 932 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 933 | |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 934 | final long now = System.currentTimeMillis(); |
| 935 | if (historyStart == Long.MAX_VALUE) historyStart = now; |
| 936 | if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 937 | |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 938 | boolean hasCycles = false; |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 939 | if (policy != null) { |
| 940 | // find the next cycle boundary |
| 941 | long cycleEnd = computeNextCycleBoundary(historyEnd, policy); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 942 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 943 | // walk backwards, generating all valid cycle ranges |
| 944 | while (cycleEnd > historyStart) { |
| 945 | final long cycleStart = computeLastCycleBoundary(cycleEnd, policy); |
| 946 | Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs=" |
| 947 | + historyStart); |
| 948 | mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd)); |
| 949 | cycleEnd = cycleStart; |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 950 | hasCycles = true; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 951 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 952 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 953 | // one last cycle entry to modify policy cycle day |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 954 | mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy)); |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 955 | } |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 956 | |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 957 | if (!hasCycles) { |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 958 | // no policy defined cycles; show entry for each four-week period |
| 959 | long cycleEnd = historyEnd; |
| 960 | while (cycleEnd > historyStart) { |
| 961 | final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4); |
| 962 | mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd)); |
| 963 | cycleEnd = cycleStart; |
| 964 | } |
| 965 | |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 966 | mCycleAdapter.setChangePossible(false); |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 967 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 968 | |
| 969 | // force pick the current cycle (first item) |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 970 | if (mCycleAdapter.getCount() > 0) { |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 971 | final int position = mCycleAdapter.findNearestPosition(previousItem); |
| 972 | mCycleSpinner.setSelection(position); |
| 973 | |
| 974 | // only force-update cycle when changed; skipping preserves any |
| 975 | // user-defined inspection region. |
| 976 | final CycleItem selectedItem = mCycleAdapter.getItem(position); |
| 977 | if (!Objects.equal(selectedItem, previousItem)) { |
| 978 | mCycleListener.onItemSelected(mCycleSpinner, null, position, 0); |
| 979 | } else { |
| 980 | // but still kick off loader for detailed list |
| 981 | updateDetailData(); |
| 982 | } |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 983 | } else { |
| 984 | updateDetailData(); |
| 985 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 986 | } |
| 987 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 988 | private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 989 | /** {@inheritDoc} */ |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 990 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 991 | if (mBinding) return; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 992 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 993 | final boolean dataEnabled = isChecked; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 994 | final String currentTab = mCurrentTab; |
| 995 | if (TAB_MOBILE.equals(currentTab)) { |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 996 | if (dataEnabled) { |
| 997 | setMobileDataEnabled(true); |
| 998 | } else { |
| 999 | // disabling data; show confirmation dialog which eventually |
| 1000 | // calls setMobileDataEnabled() once user confirms. |
| 1001 | ConfirmDataDisableFragment.show(DataUsageSummary.this); |
| 1002 | } |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 1003 | } |
Jeff Sharkey | 1ae43f9 | 2011-08-03 17:16:09 -0700 | [diff] [blame] | 1004 | |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 1005 | updatePolicy(false); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1006 | } |
| 1007 | }; |
| 1008 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 1009 | private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1010 | /** {@inheritDoc} */ |
| 1011 | public void onClick(View v) { |
| 1012 | final boolean disableAtLimit = !mDisableAtLimit.isChecked(); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1013 | if (disableAtLimit) { |
| 1014 | // enabling limit; show confirmation dialog which eventually |
| 1015 | // calls setPolicyLimitBytes() once user confirms. |
| 1016 | ConfirmLimitFragment.show(DataUsageSummary.this); |
| 1017 | } else { |
| 1018 | setPolicyLimitBytes(LIMIT_DISABLED); |
| 1019 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1020 | } |
| 1021 | }; |
| 1022 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1023 | private View.OnClickListener mAppRestrictListener = new View.OnClickListener() { |
| 1024 | /** {@inheritDoc} */ |
| 1025 | public void onClick(View v) { |
| 1026 | final boolean restrictBackground = !mAppRestrict.isChecked(); |
| 1027 | |
| 1028 | if (restrictBackground) { |
Jeff Sharkey | 3038c52 | 2011-11-30 15:37:51 -0800 | [diff] [blame] | 1029 | // enabling restriction; show confirmation dialog which |
| 1030 | // eventually calls setRestrictBackground() once user |
| 1031 | // confirms. |
| 1032 | ConfirmAppRestrictFragment.show(DataUsageSummary.this); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1033 | } else { |
| 1034 | setAppRestrictBackground(false); |
| 1035 | } |
| 1036 | } |
| 1037 | }; |
| 1038 | |
| 1039 | private OnClickListener mAppSettingsListener = new OnClickListener() { |
| 1040 | /** {@inheritDoc} */ |
| 1041 | public void onClick(View v) { |
| 1042 | // TODO: target torwards entire UID instead of just first package |
| 1043 | startActivity(mAppSettingsIntent); |
| 1044 | } |
| 1045 | }; |
| 1046 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1047 | private OnItemClickListener mListListener = new OnItemClickListener() { |
| 1048 | /** {@inheritDoc} */ |
| 1049 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 1050 | final Context context = view.getContext(); |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 1051 | final AppItem app = (AppItem) parent.getItemAtPosition(position); |
| 1052 | final UidDetail detail = mUidDetailProvider.getUidDetail(app.appId, true); |
| 1053 | AppDetailsFragment.show(DataUsageSummary.this, app, detail.label); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1054 | } |
| 1055 | }; |
| 1056 | |
| 1057 | private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() { |
| 1058 | /** {@inheritDoc} */ |
| 1059 | public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
| 1060 | final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position); |
| 1061 | if (cycle instanceof CycleChangeItem) { |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1062 | // show cycle editor; will eventually call setPolicyCycleDay() |
| 1063 | // when user finishes editing. |
| 1064 | CycleEditorFragment.show(DataUsageSummary.this); |
| 1065 | |
| 1066 | // reset spinner to something other than "change cycle..." |
| 1067 | mCycleSpinner.setSelection(0); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1068 | |
| 1069 | } else { |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 1070 | if (LOGD) { |
| 1071 | Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end=" |
| 1072 | + cycle.end + "]"); |
| 1073 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1074 | |
| 1075 | // update chart to show selected cycle, and update detail data |
| 1076 | // to match updated sweep bounds. |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1077 | mChart.setVisibleRange(cycle.start, cycle.end); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1078 | |
| 1079 | updateDetailData(); |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | /** {@inheritDoc} */ |
| 1084 | public void onNothingSelected(AdapterView<?> parent) { |
| 1085 | // ignored |
| 1086 | } |
| 1087 | }; |
| 1088 | |
| 1089 | /** |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1090 | * Update details based on {@link #mChart} inspection range depending on |
| 1091 | * current mode. In network mode, updates {@link #mAdapter} with sorted list |
| 1092 | * of applications data usage, and when {@link #isAppDetailMode()} update |
| 1093 | * app details. |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1094 | */ |
| 1095 | private void updateDetailData() { |
| 1096 | if (LOGD) Log.d(TAG, "updateDetailData()"); |
| 1097 | |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1098 | final long start = mChart.getInspectStart(); |
| 1099 | final long end = mChart.getInspectEnd(); |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1100 | final long now = System.currentTimeMillis(); |
| 1101 | |
| 1102 | final Context context = getActivity(); |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1103 | |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1104 | NetworkStatsHistory.Entry entry = null; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1105 | if (isAppDetailMode() && mChartData != null && mChartData.detail != null) { |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1106 | // bind foreground/background to piechart and labels |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1107 | entry = mChartData.detailDefault.getValues(start, end, now, entry); |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1108 | final long defaultBytes = entry.rxBytes + entry.txBytes; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1109 | entry = mChartData.detailForeground.getValues(start, end, now, entry); |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1110 | final long foregroundBytes = entry.rxBytes + entry.txBytes; |
| 1111 | |
| 1112 | mAppPieChart.setOriginAngle(175); |
| 1113 | |
| 1114 | mAppPieChart.removeAllSlices(); |
| 1115 | mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a")); |
| 1116 | mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666")); |
| 1117 | |
| 1118 | mAppPieChart.generatePath(); |
| 1119 | |
| 1120 | mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes)); |
| 1121 | mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes)); |
| 1122 | |
| 1123 | // and finally leave with summary data for label below |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1124 | entry = mChartData.detail.getValues(start, end, now, null); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1125 | |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1126 | getLoaderManager().destroyLoader(LOADER_SUMMARY); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1127 | |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1128 | } else { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1129 | if (mChartData != null) { |
| 1130 | entry = mChartData.network.getValues(start, end, now, null); |
| 1131 | } |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1132 | |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1133 | // kick off loader for detailed stats |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1134 | getLoaderManager().restartLoader(LOADER_SUMMARY, |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1135 | SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks); |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1136 | } |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1137 | |
| 1138 | final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0; |
| 1139 | final String totalPhrase = Formatter.formatFileSize(context, totalBytes); |
Jeff Sharkey | e5223a0 | 2012-03-09 17:11:14 -0800 | [diff] [blame] | 1140 | final String rangePhrase = formatDateRange(context, start, end); |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1141 | |
| 1142 | mUsageSummary.setText( |
| 1143 | getString(R.string.data_usage_total_during_range, totalPhrase, rangePhrase)); |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1146 | private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks< |
| 1147 | ChartData>() { |
| 1148 | /** {@inheritDoc} */ |
| 1149 | public Loader<ChartData> onCreateLoader(int id, Bundle args) { |
| 1150 | return new ChartDataLoader(getActivity(), mStatsService, args); |
| 1151 | } |
| 1152 | |
| 1153 | /** {@inheritDoc} */ |
| 1154 | public void onLoadFinished(Loader<ChartData> loader, ChartData data) { |
| 1155 | mChartData = data; |
| 1156 | mChart.bindNetworkStats(mChartData.network); |
| 1157 | mChart.bindDetailNetworkStats(mChartData.detail); |
| 1158 | |
| 1159 | // calcuate policy cycles based on available data |
| 1160 | updatePolicy(true); |
| 1161 | updateAppDetail(); |
| 1162 | |
| 1163 | // force scroll to top of body when showing detail |
| 1164 | if (mChartData.detail != null) { |
| 1165 | mListView.smoothScrollToPosition(0); |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | /** {@inheritDoc} */ |
| 1170 | public void onLoaderReset(Loader<ChartData> loader) { |
| 1171 | mChartData = null; |
| 1172 | mChart.bindNetworkStats(null); |
| 1173 | mChart.bindDetailNetworkStats(null); |
| 1174 | } |
| 1175 | }; |
| 1176 | |
| 1177 | private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks< |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1178 | NetworkStats>() { |
| 1179 | /** {@inheritDoc} */ |
| 1180 | public Loader<NetworkStats> onCreateLoader(int id, Bundle args) { |
| 1181 | return new SummaryForAllUidLoader(getActivity(), mStatsService, args); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1182 | } |
| 1183 | |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1184 | /** {@inheritDoc} */ |
| 1185 | public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) { |
| 1186 | mAdapter.bindStats(data); |
Jeff Sharkey | e2afc0f | 2011-08-01 15:29:30 -0700 | [diff] [blame] | 1187 | updateEmptyVisible(); |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1188 | } |
Jeff Sharkey | aa5260e | 2011-06-14 23:21:59 -0700 | [diff] [blame] | 1189 | |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1190 | /** {@inheritDoc} */ |
| 1191 | public void onLoaderReset(Loader<NetworkStats> loader) { |
| 1192 | mAdapter.bindStats(null); |
Jeff Sharkey | e2afc0f | 2011-08-01 15:29:30 -0700 | [diff] [blame] | 1193 | updateEmptyVisible(); |
| 1194 | } |
| 1195 | |
| 1196 | private void updateEmptyVisible() { |
| 1197 | final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode(); |
| 1198 | mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE); |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1199 | } |
| 1200 | }; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1201 | |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 1202 | private boolean isMobilePolicySplit() { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 1203 | final Context context = getActivity(); |
| 1204 | if (hasMobileRadio(context)) { |
| 1205 | final String subscriberId = getActiveSubscriberId(context); |
| 1206 | return mPolicyEditor.isMobilePolicySplit(subscriberId); |
| 1207 | } else { |
| 1208 | return false; |
| 1209 | } |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | private void setMobilePolicySplit(boolean split) { |
| 1213 | final String subscriberId = getActiveSubscriberId(getActivity()); |
| 1214 | mPolicyEditor.setMobilePolicySplit(subscriberId, split); |
| 1215 | } |
| 1216 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 1217 | private static String getActiveSubscriberId(Context context) { |
| 1218 | final TelephonyManager telephony = (TelephonyManager) context.getSystemService( |
| 1219 | Context.TELEPHONY_SERVICE); |
Jeff Sharkey | f3871fb | 2012-02-03 19:27:07 -0800 | [diff] [blame] | 1220 | final String actualSubscriberId = telephony.getSubscriberId(); |
| 1221 | return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId); |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1224 | private DataUsageChartListener mChartListener = new DataUsageChartListener() { |
| 1225 | /** {@inheritDoc} */ |
| 1226 | public void onInspectRangeChanged() { |
| 1227 | if (LOGD) Log.d(TAG, "onInspectRangeChanged()"); |
| 1228 | updateDetailData(); |
| 1229 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1230 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1231 | /** {@inheritDoc} */ |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 1232 | public void onWarningChanged() { |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1233 | setPolicyWarningBytes(mChart.getWarningBytes()); |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 1234 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1235 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 1236 | /** {@inheritDoc} */ |
| 1237 | public void onLimitChanged() { |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1238 | setPolicyLimitBytes(mChart.getLimitBytes()); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1239 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1240 | |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1241 | /** {@inheritDoc} */ |
| 1242 | public void requestWarningEdit() { |
| 1243 | WarningEditorFragment.show(DataUsageSummary.this); |
| 1244 | } |
| 1245 | |
| 1246 | /** {@inheritDoc} */ |
| 1247 | public void requestLimitEdit() { |
| 1248 | LimitEditorFragment.show(DataUsageSummary.this); |
| 1249 | } |
| 1250 | }; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1251 | |
| 1252 | /** |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1253 | * List item that reflects a specific data usage cycle. |
| 1254 | */ |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1255 | public static class CycleItem implements Comparable<CycleItem> { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1256 | public CharSequence label; |
| 1257 | public long start; |
| 1258 | public long end; |
| 1259 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1260 | CycleItem(CharSequence label) { |
| 1261 | this.label = label; |
| 1262 | } |
| 1263 | |
| 1264 | public CycleItem(Context context, long start, long end) { |
Jeff Sharkey | e5223a0 | 2012-03-09 17:11:14 -0800 | [diff] [blame] | 1265 | this.label = formatDateRange(context, start, end); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1266 | this.start = start; |
| 1267 | this.end = end; |
| 1268 | } |
| 1269 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1270 | @Override |
| 1271 | public String toString() { |
| 1272 | return label.toString(); |
| 1273 | } |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1274 | |
| 1275 | @Override |
| 1276 | public boolean equals(Object o) { |
| 1277 | if (o instanceof CycleItem) { |
| 1278 | final CycleItem another = (CycleItem) o; |
| 1279 | return start == another.start && end == another.end; |
| 1280 | } |
| 1281 | return false; |
| 1282 | } |
| 1283 | |
| 1284 | /** {@inheritDoc} */ |
| 1285 | public int compareTo(CycleItem another) { |
| 1286 | return Long.compare(start, another.start); |
| 1287 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1290 | private static final StringBuilder sBuilder = new StringBuilder(50); |
| 1291 | private static final java.util.Formatter sFormatter = new java.util.Formatter( |
| 1292 | sBuilder, Locale.getDefault()); |
| 1293 | |
Jeff Sharkey | e5223a0 | 2012-03-09 17:11:14 -0800 | [diff] [blame] | 1294 | public static String formatDateRange(Context context, long start, long end) { |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 1295 | final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH; |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1296 | |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 1297 | synchronized (sBuilder) { |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1298 | sBuilder.setLength(0); |
Jeff Sharkey | e5223a0 | 2012-03-09 17:11:14 -0800 | [diff] [blame] | 1299 | return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null) |
| 1300 | .toString(); |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1301 | } |
| 1302 | } |
| 1303 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1304 | /** |
| 1305 | * Special-case data usage cycle that triggers dialog to change |
| 1306 | * {@link NetworkPolicy#cycleDay}. |
| 1307 | */ |
| 1308 | public static class CycleChangeItem extends CycleItem { |
| 1309 | public CycleChangeItem(Context context) { |
| 1310 | super(context.getString(R.string.data_usage_change_cycle)); |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | public static class CycleAdapter extends ArrayAdapter<CycleItem> { |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1315 | private boolean mChangePossible = false; |
| 1316 | private boolean mChangeVisible = false; |
| 1317 | |
| 1318 | private final CycleChangeItem mChangeItem; |
| 1319 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1320 | public CycleAdapter(Context context) { |
| 1321 | super(context, android.R.layout.simple_spinner_item); |
| 1322 | setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1323 | mChangeItem = new CycleChangeItem(context); |
| 1324 | } |
| 1325 | |
| 1326 | public void setChangePossible(boolean possible) { |
| 1327 | mChangePossible = possible; |
| 1328 | updateChange(); |
| 1329 | } |
| 1330 | |
| 1331 | public void setChangeVisible(boolean visible) { |
| 1332 | mChangeVisible = visible; |
| 1333 | updateChange(); |
| 1334 | } |
| 1335 | |
| 1336 | private void updateChange() { |
| 1337 | remove(mChangeItem); |
| 1338 | if (mChangePossible && mChangeVisible) { |
| 1339 | add(mChangeItem); |
| 1340 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1341 | } |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1342 | |
| 1343 | /** |
| 1344 | * Find position of {@link CycleItem} in this adapter which is nearest |
| 1345 | * the given {@link CycleItem}. |
| 1346 | */ |
| 1347 | public int findNearestPosition(CycleItem target) { |
| 1348 | if (target != null) { |
| 1349 | final int count = getCount(); |
| 1350 | for (int i = count - 1; i >= 0; i--) { |
| 1351 | final CycleItem item = getItem(i); |
| 1352 | if (item instanceof CycleChangeItem) { |
| 1353 | continue; |
| 1354 | } else if (item.compareTo(target) >= 0) { |
| 1355 | return i; |
| 1356 | } |
| 1357 | } |
| 1358 | } |
| 1359 | return 0; |
| 1360 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1361 | } |
| 1362 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 1363 | public static class AppItem implements Comparable<AppItem>, Parcelable { |
| 1364 | public final int appId; |
| 1365 | public SparseBooleanArray uids = new SparseBooleanArray(); |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 1366 | public long total; |
| 1367 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 1368 | public AppItem(int appId) { |
| 1369 | this.appId = appId; |
| 1370 | } |
| 1371 | |
| 1372 | public AppItem(Parcel parcel) { |
| 1373 | appId = parcel.readInt(); |
| 1374 | uids = parcel.readSparseBooleanArray(); |
| 1375 | total = parcel.readLong(); |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | public void addUid(int uid) { |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 1379 | uids.put(uid, true); |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 1380 | } |
| 1381 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 1382 | @Override |
| 1383 | public void writeToParcel(Parcel dest, int flags) { |
| 1384 | dest.writeInt(appId); |
| 1385 | dest.writeSparseBooleanArray(uids); |
| 1386 | dest.writeLong(total); |
| 1387 | } |
| 1388 | |
| 1389 | @Override |
| 1390 | public int describeContents() { |
| 1391 | return 0; |
| 1392 | } |
| 1393 | |
| 1394 | @Override |
| 1395 | public int compareTo(AppItem another) { |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 1396 | return Long.compare(another.total, total); |
| 1397 | } |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 1398 | |
| 1399 | public static final Creator<AppItem> CREATOR = new Creator<AppItem>() { |
| 1400 | @Override |
| 1401 | public AppItem createFromParcel(Parcel in) { |
| 1402 | return new AppItem(in); |
| 1403 | } |
| 1404 | |
| 1405 | @Override |
| 1406 | public AppItem[] newArray(int size) { |
| 1407 | return new AppItem[size]; |
| 1408 | } |
| 1409 | }; |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 1410 | } |
| 1411 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1412 | /** |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1413 | * Adapter of applications, sorted by total usage descending. |
| 1414 | */ |
| 1415 | public static class DataUsageAdapter extends BaseAdapter { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1416 | private final UidDetailProvider mProvider; |
| 1417 | private final int mInsetSide; |
| 1418 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 1419 | private ArrayList<AppItem> mItems = Lists.newArrayList(); |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 1420 | private long mLargest; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1421 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1422 | public DataUsageAdapter(UidDetailProvider provider, int insetSide) { |
| 1423 | mProvider = checkNotNull(provider); |
| 1424 | mInsetSide = insetSide; |
| 1425 | } |
| 1426 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1427 | /** |
| 1428 | * Bind the given {@link NetworkStats}, or {@code null} to clear list. |
| 1429 | */ |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1430 | public void bindStats(NetworkStats stats) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1431 | mItems.clear(); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1432 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 1433 | final AppItem systemItem = new AppItem(android.os.Process.SYSTEM_UID); |
| 1434 | final SparseArray<AppItem> knownUids = new SparseArray<AppItem>(); |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1435 | |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1436 | NetworkStats.Entry entry = null; |
| 1437 | final int size = stats != null ? stats.size() : 0; |
| 1438 | for (int i = 0; i < size; i++) { |
| 1439 | entry = stats.getValues(i, entry); |
| 1440 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 1441 | final boolean isApp = UserId.isApp(entry.uid); |
| 1442 | final int appId = isApp ? UserId.getAppId(entry.uid) : entry.uid; |
| 1443 | if (isApp || appId == UID_REMOVED || appId == UID_TETHERING) { |
| 1444 | AppItem item = knownUids.get(appId); |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1445 | if (item == null) { |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 1446 | item = new AppItem(appId); |
| 1447 | knownUids.put(appId, item); |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1448 | mItems.add(item); |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1449 | } |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1450 | |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1451 | item.total += entry.rxBytes + entry.txBytes; |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 1452 | item.addUid(entry.uid); |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1453 | } else { |
| 1454 | systemItem.total += entry.rxBytes + entry.txBytes; |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 1455 | systemItem.addUid(entry.uid); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1456 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1457 | } |
| 1458 | |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1459 | if (systemItem.total > 0) { |
| 1460 | mItems.add(systemItem); |
| 1461 | } |
| 1462 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1463 | Collections.sort(mItems); |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 1464 | mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1465 | notifyDataSetChanged(); |
| 1466 | } |
| 1467 | |
| 1468 | @Override |
| 1469 | public int getCount() { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1470 | return mItems.size(); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | @Override |
| 1474 | public Object getItem(int position) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1475 | return mItems.get(position); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | @Override |
| 1479 | public long getItemId(int position) { |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 1480 | return mItems.get(position).appId; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | @Override |
| 1484 | public View getView(int position, View convertView, ViewGroup parent) { |
| 1485 | if (convertView == null) { |
| 1486 | convertView = LayoutInflater.from(parent.getContext()).inflate( |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 1487 | R.layout.data_usage_item, parent, false); |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1488 | |
| 1489 | if (mInsetSide > 0) { |
| 1490 | convertView.setPadding(mInsetSide, 0, mInsetSide, 0); |
| 1491 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | final Context context = parent.getContext(); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1495 | |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 1496 | final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1); |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 1497 | final ProgressBar progress = (ProgressBar) convertView.findViewById( |
| 1498 | android.R.id.progress); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1499 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1500 | // kick off async load of app details |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 1501 | final AppItem item = mItems.get(position); |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1502 | UidDetailTask.bindView(mProvider, item, convertView); |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 1503 | |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 1504 | text1.setText(Formatter.formatFileSize(context, item.total)); |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 1505 | |
| 1506 | final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0; |
| 1507 | progress.setProgress(percentTotal); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1508 | |
| 1509 | return convertView; |
| 1510 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1511 | } |
| 1512 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1513 | /** |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1514 | * Empty {@link Fragment} that controls display of UID details in |
| 1515 | * {@link DataUsageSummary}. |
| 1516 | */ |
| 1517 | public static class AppDetailsFragment extends Fragment { |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 1518 | private static final String EXTRA_APP = "app"; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1519 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 1520 | public static void show(DataUsageSummary parent, AppItem app, CharSequence label) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1521 | if (!parent.isAdded()) return; |
| 1522 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1523 | final Bundle args = new Bundle(); |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 1524 | args.putParcelable(EXTRA_APP, app); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1525 | |
| 1526 | final AppDetailsFragment fragment = new AppDetailsFragment(); |
| 1527 | fragment.setArguments(args); |
| 1528 | fragment.setTargetFragment(parent, 0); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1529 | final FragmentTransaction ft = parent.getFragmentManager().beginTransaction(); |
| 1530 | ft.add(fragment, TAG_APP_DETAILS); |
| 1531 | ft.addToBackStack(TAG_APP_DETAILS); |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 1532 | ft.setBreadCrumbTitle(label); |
Jeff Sharkey | 3235ddb | 2012-03-15 16:40:31 -0700 | [diff] [blame] | 1533 | ft.commitAllowingStateLoss(); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | @Override |
| 1537 | public void onStart() { |
| 1538 | super.onStart(); |
| 1539 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 1540 | target.mCurrentApp = getArguments().getParcelable(EXTRA_APP); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1541 | target.updateBody(); |
| 1542 | } |
| 1543 | |
| 1544 | @Override |
| 1545 | public void onStop() { |
| 1546 | super.onStop(); |
| 1547 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 1548 | target.mCurrentApp = null; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1549 | target.updateBody(); |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | /** |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1554 | * Dialog to request user confirmation before setting |
| 1555 | * {@link NetworkPolicy#limitBytes}. |
| 1556 | */ |
| 1557 | public static class ConfirmLimitFragment extends DialogFragment { |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1558 | private static final String EXTRA_MESSAGE = "message"; |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 1559 | private static final String EXTRA_LIMIT_BYTES = "limitBytes"; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1560 | |
| 1561 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1562 | if (!parent.isAdded()) return; |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1563 | |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1564 | final Resources res = parent.getResources(); |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1565 | final CharSequence message; |
| 1566 | final long limitBytes; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1567 | |
| 1568 | // TODO: customize default limits based on network template |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 1569 | final String currentTab = parent.mCurrentTab; |
| 1570 | if (TAB_3G.equals(currentTab)) { |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1571 | message = buildDialogMessage(res, R.string.data_usage_tab_3g); |
| 1572 | limitBytes = 5 * GB_IN_BYTES; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 1573 | } else if (TAB_4G.equals(currentTab)) { |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1574 | message = buildDialogMessage(res, R.string.data_usage_tab_4g); |
| 1575 | limitBytes = 5 * GB_IN_BYTES; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 1576 | } else if (TAB_MOBILE.equals(currentTab)) { |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1577 | message = buildDialogMessage(res, R.string.data_usage_list_mobile); |
| 1578 | limitBytes = 5 * GB_IN_BYTES; |
| 1579 | } else if (TAB_WIFI.equals(currentTab)) { |
| 1580 | message = buildDialogMessage(res, R.string.data_usage_tab_wifi); |
| 1581 | limitBytes = 5 * GB_IN_BYTES; |
| 1582 | } else { |
| 1583 | throw new IllegalArgumentException("unknown current tab: " + currentTab); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1584 | } |
| 1585 | |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1586 | final Bundle args = new Bundle(); |
| 1587 | args.putCharSequence(EXTRA_MESSAGE, message); |
| 1588 | args.putLong(EXTRA_LIMIT_BYTES, limitBytes); |
| 1589 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1590 | final ConfirmLimitFragment dialog = new ConfirmLimitFragment(); |
| 1591 | dialog.setArguments(args); |
| 1592 | dialog.setTargetFragment(parent, 0); |
| 1593 | dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT); |
| 1594 | } |
| 1595 | |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1596 | private static CharSequence buildDialogMessage(Resources res, int networkResId) { |
| 1597 | return res.getString(R.string.data_usage_limit_dialog, res.getString(networkResId)); |
| 1598 | } |
| 1599 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1600 | @Override |
| 1601 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1602 | final Context context = getActivity(); |
| 1603 | |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1604 | final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1605 | final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES); |
| 1606 | |
| 1607 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 1608 | builder.setTitle(R.string.data_usage_limit_dialog_title); |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1609 | builder.setMessage(message); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1610 | |
| 1611 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
| 1612 | public void onClick(DialogInterface dialog, int which) { |
| 1613 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1614 | if (target != null) { |
| 1615 | target.setPolicyLimitBytes(limitBytes); |
| 1616 | } |
| 1617 | } |
| 1618 | }); |
| 1619 | |
| 1620 | return builder.create(); |
| 1621 | } |
| 1622 | } |
| 1623 | |
| 1624 | /** |
| 1625 | * Dialog to edit {@link NetworkPolicy#cycleDay}. |
| 1626 | */ |
| 1627 | public static class CycleEditorFragment extends DialogFragment { |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1628 | private static final String EXTRA_TEMPLATE = "template"; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1629 | |
| 1630 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1631 | if (!parent.isAdded()) return; |
| 1632 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1633 | final Bundle args = new Bundle(); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1634 | args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1635 | |
| 1636 | final CycleEditorFragment dialog = new CycleEditorFragment(); |
| 1637 | dialog.setArguments(args); |
| 1638 | dialog.setTargetFragment(parent, 0); |
| 1639 | dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR); |
| 1640 | } |
| 1641 | |
| 1642 | @Override |
| 1643 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1644 | final Context context = getActivity(); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1645 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1646 | final NetworkPolicyEditor editor = target.mPolicyEditor; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1647 | |
| 1648 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 1649 | final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext()); |
| 1650 | |
| 1651 | final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false); |
| 1652 | final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day); |
| 1653 | |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1654 | final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE); |
| 1655 | final int cycleDay = editor.getPolicyCycleDay(template); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1656 | |
| 1657 | cycleDayPicker.setMinValue(1); |
| 1658 | cycleDayPicker.setMaxValue(31); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1659 | cycleDayPicker.setValue(cycleDay); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1660 | cycleDayPicker.setWrapSelectorWheel(true); |
| 1661 | |
| 1662 | builder.setTitle(R.string.data_usage_cycle_editor_title); |
| 1663 | builder.setView(view); |
| 1664 | |
| 1665 | builder.setPositiveButton(R.string.data_usage_cycle_editor_positive, |
| 1666 | new DialogInterface.OnClickListener() { |
| 1667 | public void onClick(DialogInterface dialog, int which) { |
| 1668 | final int cycleDay = cycleDayPicker.getValue(); |
Jeff Sharkey | e5223a0 | 2012-03-09 17:11:14 -0800 | [diff] [blame] | 1669 | final String cycleTimezone = new Time().timezone; |
| 1670 | editor.setPolicyCycleDay(template, cycleDay, cycleTimezone); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1671 | target.updatePolicy(true); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1672 | } |
| 1673 | }); |
| 1674 | |
| 1675 | return builder.create(); |
| 1676 | } |
| 1677 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1678 | |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 1679 | /** |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1680 | * Dialog to edit {@link NetworkPolicy#warningBytes}. |
| 1681 | */ |
| 1682 | public static class WarningEditorFragment extends DialogFragment { |
| 1683 | private static final String EXTRA_TEMPLATE = "template"; |
| 1684 | |
| 1685 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1686 | if (!parent.isAdded()) return; |
| 1687 | |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1688 | final Bundle args = new Bundle(); |
| 1689 | args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate); |
| 1690 | |
| 1691 | final WarningEditorFragment dialog = new WarningEditorFragment(); |
| 1692 | dialog.setArguments(args); |
| 1693 | dialog.setTargetFragment(parent, 0); |
| 1694 | dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR); |
| 1695 | } |
| 1696 | |
| 1697 | @Override |
| 1698 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1699 | final Context context = getActivity(); |
| 1700 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1701 | final NetworkPolicyEditor editor = target.mPolicyEditor; |
| 1702 | |
| 1703 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 1704 | final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext()); |
| 1705 | |
| 1706 | final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false); |
| 1707 | final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes); |
| 1708 | |
| 1709 | final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE); |
| 1710 | final long warningBytes = editor.getPolicyWarningBytes(template); |
| 1711 | final long limitBytes = editor.getPolicyLimitBytes(template); |
| 1712 | |
| 1713 | bytesPicker.setMinValue(0); |
| 1714 | if (limitBytes != LIMIT_DISABLED) { |
| 1715 | bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1); |
| 1716 | } else { |
| 1717 | bytesPicker.setMaxValue(Integer.MAX_VALUE); |
| 1718 | } |
| 1719 | bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES)); |
| 1720 | bytesPicker.setWrapSelectorWheel(false); |
| 1721 | |
| 1722 | builder.setTitle(R.string.data_usage_warning_editor_title); |
| 1723 | builder.setView(view); |
| 1724 | |
| 1725 | builder.setPositiveButton(R.string.data_usage_cycle_editor_positive, |
| 1726 | new DialogInterface.OnClickListener() { |
| 1727 | public void onClick(DialogInterface dialog, int which) { |
| 1728 | // clear focus to finish pending text edits |
| 1729 | bytesPicker.clearFocus(); |
| 1730 | |
| 1731 | final long bytes = bytesPicker.getValue() * MB_IN_BYTES; |
| 1732 | editor.setPolicyWarningBytes(template, bytes); |
| 1733 | target.updatePolicy(false); |
| 1734 | } |
| 1735 | }); |
| 1736 | |
| 1737 | return builder.create(); |
| 1738 | } |
| 1739 | } |
| 1740 | |
| 1741 | /** |
| 1742 | * Dialog to edit {@link NetworkPolicy#limitBytes}. |
| 1743 | */ |
| 1744 | public static class LimitEditorFragment extends DialogFragment { |
| 1745 | private static final String EXTRA_TEMPLATE = "template"; |
| 1746 | |
| 1747 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1748 | if (!parent.isAdded()) return; |
| 1749 | |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1750 | final Bundle args = new Bundle(); |
| 1751 | args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate); |
| 1752 | |
| 1753 | final LimitEditorFragment dialog = new LimitEditorFragment(); |
| 1754 | dialog.setArguments(args); |
| 1755 | dialog.setTargetFragment(parent, 0); |
| 1756 | dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR); |
| 1757 | } |
| 1758 | |
| 1759 | @Override |
| 1760 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1761 | final Context context = getActivity(); |
| 1762 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1763 | final NetworkPolicyEditor editor = target.mPolicyEditor; |
| 1764 | |
| 1765 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 1766 | final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext()); |
| 1767 | |
| 1768 | final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false); |
| 1769 | final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes); |
| 1770 | |
| 1771 | final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE); |
| 1772 | final long warningBytes = editor.getPolicyWarningBytes(template); |
| 1773 | final long limitBytes = editor.getPolicyLimitBytes(template); |
| 1774 | |
| 1775 | bytesPicker.setMaxValue(Integer.MAX_VALUE); |
Shuhrat Dehkanov | f9237f6 | 2012-01-26 23:04:02 +0900 | [diff] [blame] | 1776 | if (warningBytes != WARNING_DISABLED && limitBytes > 0) { |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1777 | bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1); |
| 1778 | } else { |
| 1779 | bytesPicker.setMinValue(0); |
| 1780 | } |
| 1781 | bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES)); |
| 1782 | bytesPicker.setWrapSelectorWheel(false); |
| 1783 | |
| 1784 | builder.setTitle(R.string.data_usage_limit_editor_title); |
| 1785 | builder.setView(view); |
| 1786 | |
| 1787 | builder.setPositiveButton(R.string.data_usage_cycle_editor_positive, |
| 1788 | new DialogInterface.OnClickListener() { |
| 1789 | public void onClick(DialogInterface dialog, int which) { |
| 1790 | // clear focus to finish pending text edits |
| 1791 | bytesPicker.clearFocus(); |
| 1792 | |
| 1793 | final long bytes = bytesPicker.getValue() * MB_IN_BYTES; |
| 1794 | editor.setPolicyLimitBytes(template, bytes); |
| 1795 | target.updatePolicy(false); |
| 1796 | } |
| 1797 | }); |
| 1798 | |
| 1799 | return builder.create(); |
| 1800 | } |
| 1801 | } |
| 1802 | /** |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 1803 | * Dialog to request user confirmation before disabling data. |
| 1804 | */ |
| 1805 | public static class ConfirmDataDisableFragment extends DialogFragment { |
| 1806 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1807 | if (!parent.isAdded()) return; |
| 1808 | |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 1809 | final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment(); |
| 1810 | dialog.setTargetFragment(parent, 0); |
| 1811 | dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE); |
| 1812 | } |
| 1813 | |
| 1814 | @Override |
| 1815 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1816 | final Context context = getActivity(); |
| 1817 | |
| 1818 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 1819 | builder.setMessage(R.string.data_usage_disable_mobile); |
| 1820 | |
| 1821 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
| 1822 | public void onClick(DialogInterface dialog, int which) { |
| 1823 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1824 | if (target != null) { |
| 1825 | // TODO: extend to modify policy enabled flag. |
| 1826 | target.setMobileDataEnabled(false); |
| 1827 | } |
| 1828 | } |
| 1829 | }); |
| 1830 | builder.setNegativeButton(android.R.string.cancel, null); |
| 1831 | |
| 1832 | return builder.create(); |
| 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | /** |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1837 | * Dialog to request user confirmation before setting |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1838 | * {@link Settings.Secure#DATA_ROAMING}. |
| 1839 | */ |
| 1840 | public static class ConfirmDataRoamingFragment extends DialogFragment { |
| 1841 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1842 | if (!parent.isAdded()) return; |
| 1843 | |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1844 | final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment(); |
| 1845 | dialog.setTargetFragment(parent, 0); |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 1846 | dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1847 | } |
| 1848 | |
| 1849 | @Override |
| 1850 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1851 | final Context context = getActivity(); |
| 1852 | |
| 1853 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 1854 | builder.setTitle(R.string.roaming_reenable_title); |
| 1855 | builder.setMessage(R.string.roaming_warning); |
| 1856 | |
| 1857 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
| 1858 | public void onClick(DialogInterface dialog, int which) { |
| 1859 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1860 | if (target != null) { |
| 1861 | target.setDataRoaming(true); |
| 1862 | } |
| 1863 | } |
| 1864 | }); |
| 1865 | builder.setNegativeButton(android.R.string.cancel, null); |
| 1866 | |
| 1867 | return builder.create(); |
| 1868 | } |
| 1869 | } |
| 1870 | |
| 1871 | /** |
| 1872 | * Dialog to request user confirmation before setting |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1873 | * {@link INetworkPolicyManager#setRestrictBackground(boolean)}. |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1874 | */ |
| 1875 | public static class ConfirmRestrictFragment extends DialogFragment { |
| 1876 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1877 | if (!parent.isAdded()) return; |
| 1878 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1879 | final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment(); |
| 1880 | dialog.setTargetFragment(parent, 0); |
| 1881 | dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT); |
| 1882 | } |
| 1883 | |
| 1884 | @Override |
| 1885 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1886 | final Context context = getActivity(); |
| 1887 | |
| 1888 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1889 | builder.setTitle(R.string.data_usage_restrict_background_title); |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1890 | builder.setMessage(getString(R.string.data_usage_restrict_background)); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1891 | |
| 1892 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
| 1893 | public void onClick(DialogInterface dialog, int which) { |
| 1894 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1895 | if (target != null) { |
| 1896 | target.setRestrictBackground(true); |
| 1897 | } |
| 1898 | } |
| 1899 | }); |
| 1900 | builder.setNegativeButton(android.R.string.cancel, null); |
| 1901 | |
| 1902 | return builder.create(); |
| 1903 | } |
| 1904 | } |
| 1905 | |
| 1906 | /** |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1907 | * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND} |
| 1908 | * change has been denied, usually based on |
| 1909 | * {@link DataUsageSummary#hasLimitedNetworks()}. |
| 1910 | */ |
| 1911 | public static class DeniedRestrictFragment extends DialogFragment { |
| 1912 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1913 | if (!parent.isAdded()) return; |
| 1914 | |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1915 | final DeniedRestrictFragment dialog = new DeniedRestrictFragment(); |
| 1916 | dialog.setTargetFragment(parent, 0); |
| 1917 | dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT); |
| 1918 | } |
| 1919 | |
| 1920 | @Override |
| 1921 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1922 | final Context context = getActivity(); |
| 1923 | |
| 1924 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 1925 | builder.setTitle(R.string.data_usage_app_restrict_background); |
| 1926 | builder.setMessage(R.string.data_usage_restrict_denied_dialog); |
| 1927 | builder.setPositiveButton(android.R.string.ok, null); |
| 1928 | |
| 1929 | return builder.create(); |
| 1930 | } |
| 1931 | } |
| 1932 | |
| 1933 | /** |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1934 | * Dialog to request user confirmation before setting |
| 1935 | * {@link #POLICY_REJECT_METERED_BACKGROUND}. |
| 1936 | */ |
| 1937 | public static class ConfirmAppRestrictFragment extends DialogFragment { |
| 1938 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1939 | if (!parent.isAdded()) return; |
| 1940 | |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1941 | final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment(); |
| 1942 | dialog.setTargetFragment(parent, 0); |
| 1943 | dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT); |
| 1944 | } |
| 1945 | |
| 1946 | @Override |
| 1947 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1948 | final Context context = getActivity(); |
| 1949 | |
| 1950 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1951 | builder.setTitle(R.string.data_usage_app_restrict_dialog_title); |
| 1952 | builder.setMessage(R.string.data_usage_app_restrict_dialog); |
| 1953 | |
| 1954 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
| 1955 | public void onClick(DialogInterface dialog, int which) { |
| 1956 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1957 | if (target != null) { |
| 1958 | target.setAppRestrictBackground(true); |
| 1959 | } |
| 1960 | } |
| 1961 | }); |
| 1962 | builder.setNegativeButton(android.R.string.cancel, null); |
| 1963 | |
| 1964 | return builder.create(); |
| 1965 | } |
| 1966 | } |
| 1967 | |
| 1968 | /** |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 1969 | * Compute default tab that should be selected, based on |
| 1970 | * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra. |
| 1971 | */ |
| 1972 | private static String computeTabFromIntent(Intent intent) { |
Jeff Sharkey | 271ec8a | 2011-07-20 16:59:16 -0700 | [diff] [blame] | 1973 | final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE); |
| 1974 | if (template == null) return null; |
| 1975 | |
| 1976 | switch (template.getMatchRule()) { |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 1977 | case MATCH_MOBILE_3G_LOWER: |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 1978 | return TAB_3G; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 1979 | case MATCH_MOBILE_4G: |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 1980 | return TAB_4G; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 1981 | case MATCH_MOBILE_ALL: |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 1982 | return TAB_MOBILE; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 1983 | case MATCH_WIFI: |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 1984 | return TAB_WIFI; |
| 1985 | default: |
| 1986 | return null; |
| 1987 | } |
| 1988 | } |
| 1989 | |
| 1990 | /** |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1991 | * Background task that loads {@link UidDetail}, binding to |
| 1992 | * {@link DataUsageAdapter} row item when finished. |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 1993 | */ |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1994 | private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> { |
| 1995 | private final UidDetailProvider mProvider; |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 1996 | private final AppItem mItem; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1997 | private final View mTarget; |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1998 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 1999 | private UidDetailTask(UidDetailProvider provider, AppItem item, View target) { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2000 | mProvider = checkNotNull(provider); |
| 2001 | mItem = checkNotNull(item); |
| 2002 | mTarget = checkNotNull(target); |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 2003 | } |
| 2004 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2005 | public static void bindView( |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 2006 | UidDetailProvider provider, AppItem item, View target) { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2007 | final UidDetailTask existing = (UidDetailTask) target.getTag(); |
| 2008 | if (existing != null) { |
| 2009 | existing.cancel(false); |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 2010 | } |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2011 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 2012 | final UidDetail cachedDetail = provider.getUidDetail(item.appId, false); |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2013 | if (cachedDetail != null) { |
| 2014 | bindView(cachedDetail, target); |
| 2015 | } else { |
| 2016 | target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor( |
| 2017 | AsyncTask.THREAD_POOL_EXECUTOR)); |
| 2018 | } |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 2019 | } |
| 2020 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2021 | private static void bindView(UidDetail detail, View target) { |
| 2022 | final ImageView icon = (ImageView) target.findViewById(android.R.id.icon); |
| 2023 | final TextView title = (TextView) target.findViewById(android.R.id.title); |
| 2024 | |
| 2025 | if (detail != null) { |
| 2026 | icon.setImageDrawable(detail.icon); |
| 2027 | title.setText(detail.label); |
| 2028 | } else { |
| 2029 | icon.setImageDrawable(null); |
| 2030 | title.setText(null); |
| 2031 | } |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 2032 | } |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2033 | |
| 2034 | @Override |
| 2035 | protected void onPreExecute() { |
| 2036 | bindView(null, mTarget); |
| 2037 | } |
| 2038 | |
| 2039 | @Override |
| 2040 | protected UidDetail doInBackground(Void... params) { |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame^] | 2041 | return mProvider.getUidDetail(mItem.appId, true); |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2042 | } |
| 2043 | |
| 2044 | @Override |
| 2045 | protected void onPostExecute(UidDetail result) { |
| 2046 | bindView(result, mTarget); |
| 2047 | } |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 2048 | } |
| 2049 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2050 | /** |
| 2051 | * Test if device has a mobile data radio. |
| 2052 | */ |
| 2053 | private static boolean hasMobileRadio(Context context) { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 2054 | if (TEST_RADIOS) { |
| 2055 | return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile"); |
| 2056 | } |
| 2057 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2058 | final ConnectivityManager conn = (ConnectivityManager) context.getSystemService( |
| 2059 | Context.CONNECTIVITY_SERVICE); |
Jeff Sharkey | bdf98e8 | 2011-11-10 17:17:24 -0800 | [diff] [blame] | 2060 | return conn.isNetworkSupported(TYPE_MOBILE); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | /** |
| 2064 | * Test if device has a mobile 4G data radio. |
| 2065 | */ |
| 2066 | private static boolean hasMobile4gRadio(Context context) { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2067 | if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) { |
| 2068 | return false; |
| 2069 | } |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 2070 | if (TEST_RADIOS) { |
| 2071 | return SystemProperties.get(TEST_RADIOS_PROP).contains("4g"); |
| 2072 | } |
| 2073 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2074 | final ConnectivityManager conn = (ConnectivityManager) context.getSystemService( |
| 2075 | Context.CONNECTIVITY_SERVICE); |
| 2076 | final TelephonyManager telephony = (TelephonyManager) context.getSystemService( |
| 2077 | Context.TELEPHONY_SERVICE); |
| 2078 | |
Jeff Sharkey | bdf98e8 | 2011-11-10 17:17:24 -0800 | [diff] [blame] | 2079 | final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2080 | final boolean hasLte = telephony.getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE; |
| 2081 | return hasWimax || hasLte; |
| 2082 | } |
| 2083 | |
| 2084 | /** |
| 2085 | * Test if device has a Wi-Fi data radio. |
| 2086 | */ |
| 2087 | private static boolean hasWifiRadio(Context context) { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 2088 | if (TEST_RADIOS) { |
| 2089 | return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi"); |
| 2090 | } |
| 2091 | |
Jeff Sharkey | bdf98e8 | 2011-11-10 17:17:24 -0800 | [diff] [blame] | 2092 | final ConnectivityManager conn = (ConnectivityManager) context.getSystemService( |
| 2093 | Context.CONNECTIVITY_SERVICE); |
| 2094 | return conn.isNetworkSupported(TYPE_WIFI); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2095 | } |
| 2096 | |
| 2097 | /** |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 2098 | * Test if device has an ethernet network connection. |
| 2099 | */ |
| 2100 | private static boolean hasEthernet(Context context) { |
| 2101 | if (TEST_RADIOS) { |
| 2102 | return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet"); |
| 2103 | } |
| 2104 | |
| 2105 | final ConnectivityManager conn = (ConnectivityManager) context.getSystemService( |
| 2106 | Context.CONNECTIVITY_SERVICE); |
Jeff Sharkey | bdf98e8 | 2011-11-10 17:17:24 -0800 | [diff] [blame] | 2107 | return conn.isNetworkSupported(TYPE_ETHERNET); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | /** |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2111 | * Inflate a {@link Preference} style layout, adding the given {@link View} |
| 2112 | * widget into {@link android.R.id#widget_frame}. |
| 2113 | */ |
| 2114 | private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) { |
| 2115 | final View view = inflater.inflate(R.layout.preference, root, false); |
| 2116 | final LinearLayout widgetFrame = (LinearLayout) view.findViewById( |
| 2117 | android.R.id.widget_frame); |
| 2118 | widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT)); |
| 2119 | return view; |
| 2120 | } |
| 2121 | |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 2122 | private static View inflateAppTitle( |
| 2123 | LayoutInflater inflater, ViewGroup root, CharSequence label) { |
| 2124 | final TextView view = (TextView) inflater.inflate( |
| 2125 | R.layout.data_usage_app_title, root, false); |
| 2126 | view.setText(label); |
| 2127 | return view; |
| 2128 | } |
| 2129 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2130 | /** |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 2131 | * Test if any networks are currently limited. |
| 2132 | */ |
| 2133 | private boolean hasLimitedNetworks() { |
| 2134 | return !buildLimitedNetworksList().isEmpty(); |
| 2135 | } |
| 2136 | |
| 2137 | /** |
Jeff Sharkey | 30dde0f | 2011-08-17 15:08:12 -0700 | [diff] [blame] | 2138 | * Build string describing currently limited networks, which defines when |
| 2139 | * background data is restricted. |
| 2140 | */ |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 2141 | private CharSequence buildLimitedNetworksString() { |
| 2142 | final List<CharSequence> limited = buildLimitedNetworksList(); |
| 2143 | |
| 2144 | // handle case where no networks limited |
| 2145 | if (limited.isEmpty()) { |
| 2146 | limited.add(getText(R.string.data_usage_list_none)); |
| 2147 | } |
| 2148 | |
| 2149 | return TextUtils.join(limited); |
| 2150 | } |
| 2151 | |
| 2152 | /** |
| 2153 | * Build list of currently limited networks, which defines when background |
| 2154 | * data is restricted. |
| 2155 | */ |
| 2156 | private List<CharSequence> buildLimitedNetworksList() { |
Jeff Sharkey | 30dde0f | 2011-08-17 15:08:12 -0700 | [diff] [blame] | 2157 | final Context context = getActivity(); |
| 2158 | final String subscriberId = getActiveSubscriberId(context); |
| 2159 | |
| 2160 | // build combined list of all limited networks |
| 2161 | final ArrayList<CharSequence> limited = Lists.newArrayList(); |
| 2162 | if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) { |
| 2163 | limited.add(getText(R.string.data_usage_list_mobile)); |
| 2164 | } |
| 2165 | if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) { |
| 2166 | limited.add(getText(R.string.data_usage_tab_3g)); |
| 2167 | } |
| 2168 | if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) { |
| 2169 | limited.add(getText(R.string.data_usage_tab_4g)); |
| 2170 | } |
| 2171 | if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifi())) { |
| 2172 | limited.add(getText(R.string.data_usage_tab_wifi)); |
| 2173 | } |
| 2174 | if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) { |
| 2175 | limited.add(getText(R.string.data_usage_tab_ethernet)); |
| 2176 | } |
| 2177 | |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 2178 | return limited; |
Jeff Sharkey | 30dde0f | 2011-08-17 15:08:12 -0700 | [diff] [blame] | 2179 | } |
| 2180 | |
| 2181 | /** |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 2182 | * Inset both selector and divider {@link Drawable} on the given |
| 2183 | * {@link ListView} by the requested dimensions. |
| 2184 | */ |
| 2185 | private static void insetListViewDrawables(ListView view, int insetSide) { |
| 2186 | final Drawable selector = view.getSelector(); |
| 2187 | final Drawable divider = view.getDivider(); |
| 2188 | |
| 2189 | // fully unregister these drawables so callbacks can be maintained after |
| 2190 | // wrapping below. |
| 2191 | final Drawable stub = new ColorDrawable(Color.TRANSPARENT); |
| 2192 | view.setSelector(stub); |
| 2193 | view.setDivider(stub); |
| 2194 | |
| 2195 | view.setSelector(new InsetBoundsDrawable(selector, insetSide)); |
| 2196 | view.setDivider(new InsetBoundsDrawable(divider, insetSide)); |
| 2197 | } |
| 2198 | |
| 2199 | /** |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2200 | * Set {@link android.R.id#title} for a preference view inflated with |
Jeff Sharkey | 52c3f44 | 2011-06-23 00:39:38 -0700 | [diff] [blame] | 2201 | * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}. |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2202 | */ |
| 2203 | private static void setPreferenceTitle(View parent, int resId) { |
| 2204 | final TextView title = (TextView) parent.findViewById(android.R.id.title); |
| 2205 | title.setText(resId); |
| 2206 | } |
| 2207 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 2208 | /** |
| 2209 | * Set {@link android.R.id#summary} for a preference view inflated with |
| 2210 | * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}. |
| 2211 | */ |
Jeff Sharkey | 30dde0f | 2011-08-17 15:08:12 -0700 | [diff] [blame] | 2212 | private static void setPreferenceSummary(View parent, CharSequence string) { |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 2213 | final TextView summary = (TextView) parent.findViewById(android.R.id.summary); |
| 2214 | summary.setVisibility(View.VISIBLE); |
Jeff Sharkey | 30dde0f | 2011-08-17 15:08:12 -0700 | [diff] [blame] | 2215 | summary.setText(string); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 2216 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 2217 | } |