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; |
| 21 | import static android.net.ConnectivityManager.TYPE_WIMAX; |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 22 | import static android.net.NetworkPolicy.LIMIT_DISABLED; |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 23 | import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 24 | import static android.net.NetworkPolicyManager.POLICY_NONE; |
| 25 | import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 26 | import static android.net.NetworkPolicyManager.computeLastCycleBoundary; |
| 27 | import static android.net.NetworkPolicyManager.computeNextCycleBoundary; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 28 | import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER; |
| 29 | import static android.net.NetworkTemplate.MATCH_MOBILE_4G; |
| 30 | import static android.net.NetworkTemplate.MATCH_MOBILE_ALL; |
| 31 | import static android.net.NetworkTemplate.MATCH_WIFI; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 32 | import static android.net.NetworkTemplate.buildTemplateEthernet; |
| 33 | import static android.net.NetworkTemplate.buildTemplateMobile3gLower; |
| 34 | import static android.net.NetworkTemplate.buildTemplateMobile4g; |
| 35 | import static android.net.NetworkTemplate.buildTemplateMobileAll; |
| 36 | import static android.net.NetworkTemplate.buildTemplateWifi; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 37 | import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 38 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 39 | import android.animation.LayoutTransition; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 40 | import android.app.AlertDialog; |
| 41 | import android.app.Dialog; |
| 42 | import android.app.DialogFragment; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 43 | import android.app.Fragment; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 44 | import android.app.FragmentTransaction; |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 45 | import android.app.LoaderManager.LoaderCallbacks; |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 46 | import android.content.ContentResolver; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 47 | import android.content.Context; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 48 | import android.content.DialogInterface; |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 49 | import android.content.Intent; |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 50 | import android.content.Loader; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 51 | import android.content.SharedPreferences; |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 52 | import android.content.pm.ApplicationInfo; |
| 53 | import android.content.pm.PackageInfo; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 54 | import android.content.pm.PackageManager; |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 55 | import android.content.pm.PackageManager.NameNotFoundException; |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 56 | import android.content.res.Resources; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 57 | import android.net.ConnectivityManager; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 58 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 59 | import android.net.INetworkStatsService; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 60 | import android.net.NetworkPolicy; |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 61 | import android.net.NetworkPolicyManager; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 62 | import android.net.NetworkStats; |
| 63 | import android.net.NetworkStatsHistory; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 64 | import android.net.NetworkTemplate; |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 65 | import android.net.TrafficStats; |
Jeff Sharkey | aa5260e | 2011-06-14 23:21:59 -0700 | [diff] [blame] | 66 | import android.os.AsyncTask; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 67 | import android.os.Bundle; |
| 68 | import android.os.RemoteException; |
| 69 | import android.os.ServiceManager; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 70 | import android.os.SystemProperties; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 71 | import android.preference.Preference; |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 72 | import android.provider.Settings; |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 73 | import android.telephony.TelephonyManager; |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 74 | import android.text.TextUtils; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 75 | import android.text.format.DateUtils; |
| 76 | import android.text.format.Formatter; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 77 | import android.text.format.Time; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 78 | import android.util.Log; |
| 79 | import android.view.LayoutInflater; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 80 | import android.view.Menu; |
| 81 | import android.view.MenuInflater; |
| 82 | import android.view.MenuItem; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 83 | import android.view.View; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 84 | import android.view.View.OnClickListener; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 85 | import android.view.ViewGroup; |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 86 | import android.view.ViewTreeObserver.OnGlobalLayoutListener; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 87 | import android.widget.AdapterView; |
| 88 | import android.widget.AdapterView.OnItemClickListener; |
| 89 | import android.widget.AdapterView.OnItemSelectedListener; |
| 90 | import android.widget.ArrayAdapter; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 91 | import android.widget.BaseAdapter; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 92 | import android.widget.Button; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 93 | import android.widget.CheckBox; |
| 94 | import android.widget.CompoundButton; |
| 95 | import android.widget.CompoundButton.OnCheckedChangeListener; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 96 | import android.widget.LinearLayout; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 97 | import android.widget.ListView; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 98 | import android.widget.NumberPicker; |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 99 | import android.widget.ProgressBar; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 100 | import android.widget.Spinner; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 101 | import android.widget.Switch; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 102 | import android.widget.TabHost; |
| 103 | import android.widget.TabHost.OnTabChangeListener; |
| 104 | import android.widget.TabHost.TabContentFactory; |
| 105 | import android.widget.TabHost.TabSpec; |
| 106 | import android.widget.TabWidget; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 107 | import android.widget.TextView; |
| 108 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 109 | import com.android.internal.telephony.Phone; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 110 | import com.android.settings.net.NetworkPolicyEditor; |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 111 | import com.android.settings.net.SummaryForAllUidLoader; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 112 | import com.android.settings.widget.DataUsageChartView; |
| 113 | import com.android.settings.widget.DataUsageChartView.DataUsageChartListener; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 114 | import com.google.android.collect.Lists; |
| 115 | |
| 116 | import java.util.ArrayList; |
| 117 | import java.util.Collections; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 118 | import java.util.Locale; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 119 | |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 120 | import libcore.util.Objects; |
| 121 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 122 | /** |
| 123 | * Panel show data usage history across various networks, including options to |
| 124 | * inspect based on usage cycle and control through {@link NetworkPolicy}. |
| 125 | */ |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 126 | public class DataUsageSummary extends Fragment { |
| 127 | private static final String TAG = "DataUsage"; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 128 | private static final boolean LOGD = true; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 129 | |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 130 | // TODO: remove this testing code |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 131 | private static final boolean TEST_ANIM = false; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 132 | private static final boolean TEST_RADIOS = false; |
| 133 | private static final String TEST_RADIOS_PROP = "test.radios"; |
| 134 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 135 | private static final String TAB_3G = "3g"; |
| 136 | private static final String TAB_4G = "4g"; |
| 137 | private static final String TAB_MOBILE = "mobile"; |
| 138 | private static final String TAB_WIFI = "wifi"; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 139 | private static final String TAB_ETHERNET = "ethernet"; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 140 | |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 141 | private static final String TAG_CONFIRM_ROAMING = "confirmRoaming"; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 142 | private static final String TAG_CONFIRM_LIMIT = "confirmLimit"; |
| 143 | private static final String TAG_CYCLE_EDITOR = "cycleEditor"; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 144 | private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict"; |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 145 | private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict"; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 146 | private static final String TAG_APP_DETAILS = "appDetails"; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 147 | |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 148 | private static final int LOADER_SUMMARY = 2; |
| 149 | |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 150 | private static final long KB_IN_BYTES = 1024; |
| 151 | private static final long MB_IN_BYTES = KB_IN_BYTES * 1024; |
| 152 | private static final long GB_IN_BYTES = MB_IN_BYTES * 1024; |
| 153 | |
| 154 | private INetworkStatsService mStatsService; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 155 | private INetworkPolicyManager mPolicyService; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 156 | private ConnectivityManager mConnService; |
| 157 | |
| 158 | private static final String PREF_FILE = "data_usage"; |
| 159 | private static final String PREF_SHOW_WIFI = "show_wifi"; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 160 | private static final String PREF_SHOW_ETHERNET = "show_ethernet"; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 161 | |
| 162 | private SharedPreferences mPrefs; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 163 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 164 | private TabHost mTabHost; |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 165 | private ViewGroup mTabsContainer; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 166 | private TabWidget mTabWidget; |
| 167 | private ListView mListView; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 168 | private DataUsageAdapter mAdapter; |
| 169 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 170 | private ViewGroup mHeader; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 171 | |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 172 | private ViewGroup mNetworkSwitchesContainer; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 173 | private LinearLayout mNetworkSwitches; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 174 | private Switch mDataEnabled; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 175 | private View mDataEnabledView; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 176 | private CheckBox mDisableAtLimit; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 177 | private View mDisableAtLimitView; |
| 178 | |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 179 | private View mCycleView; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 180 | private Spinner mCycleSpinner; |
| 181 | private CycleAdapter mCycleAdapter; |
| 182 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 183 | private DataUsageChartView mChart; |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 184 | private TextView mUsageSummary; |
Jeff Sharkey | e2afc0f | 2011-08-01 15:29:30 -0700 | [diff] [blame] | 185 | private TextView mEmpty; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 186 | |
| 187 | private View mAppDetail; |
| 188 | private TextView mAppTitle; |
| 189 | private TextView mAppSubtitle; |
| 190 | private Button mAppSettings; |
| 191 | |
| 192 | private LinearLayout mAppSwitches; |
| 193 | private CheckBox mAppRestrict; |
| 194 | private View mAppRestrictView; |
| 195 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 196 | private boolean mShowWifi = false; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 197 | private boolean mShowEthernet = false; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 198 | |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 199 | private NetworkTemplate mTemplate = null; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 200 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 201 | private static final int UID_NONE = -1; |
| 202 | private int mUid = UID_NONE; |
| 203 | |
| 204 | private Intent mAppSettingsIntent; |
| 205 | |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 206 | private NetworkPolicyEditor mPolicyEditor; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 207 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 208 | private NetworkStatsHistory mHistory; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 209 | private NetworkStatsHistory mDetailHistory; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 210 | |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 211 | private String mCurrentTab = null; |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 212 | private String mIntentTab = null; |
| 213 | |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 214 | private MenuItem mMenuDataRoaming; |
| 215 | private MenuItem mMenuRestrictBackground; |
| 216 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 217 | /** Flag used to ignore listeners during binding. */ |
| 218 | private boolean mBinding; |
| 219 | |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 220 | @Override |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 221 | public void onCreate(Bundle savedInstanceState) { |
| 222 | super.onCreate(savedInstanceState); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 223 | |
| 224 | mStatsService = INetworkStatsService.Stub.asInterface( |
| 225 | ServiceManager.getService(Context.NETWORK_STATS_SERVICE)); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 226 | mPolicyService = INetworkPolicyManager.Stub.asInterface( |
| 227 | ServiceManager.getService(Context.NETWORK_POLICY_SERVICE)); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 228 | mConnService = (ConnectivityManager) getActivity().getSystemService( |
| 229 | Context.CONNECTIVITY_SERVICE); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 230 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 231 | mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE); |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 232 | |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 233 | mPolicyEditor = new NetworkPolicyEditor(mPolicyService); |
| 234 | mPolicyEditor.read(); |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 235 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 236 | mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 237 | mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 238 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 239 | setHasOptionsMenu(true); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 240 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 241 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 242 | @Override |
| 243 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 244 | Bundle savedInstanceState) { |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 245 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 246 | final Context context = inflater.getContext(); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 247 | final View view = inflater.inflate(R.layout.data_usage_summary, container, false); |
| 248 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 249 | mTabHost = (TabHost) view.findViewById(android.R.id.tabhost); |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 250 | mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 251 | mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs); |
| 252 | mListView = (ListView) view.findViewById(android.R.id.list); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 253 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 254 | mTabHost.setup(); |
| 255 | mTabHost.setOnTabChangedListener(mTabListener); |
| 256 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 257 | mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 258 | mListView.addHeaderView(mHeader, null, false); |
| 259 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 260 | { |
| 261 | // bind network switches |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 262 | mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById( |
| 263 | R.id.network_switches_container); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 264 | mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 265 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 266 | mDataEnabled = new Switch(inflater.getContext()); |
| 267 | mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled); |
| 268 | mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener); |
| 269 | mNetworkSwitches.addView(mDataEnabledView); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 270 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 271 | mDisableAtLimit = new CheckBox(inflater.getContext()); |
| 272 | mDisableAtLimit.setClickable(false); |
| 273 | mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit); |
| 274 | mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener); |
| 275 | mNetworkSwitches.addView(mDisableAtLimitView); |
| 276 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 277 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 278 | // bind cycle dropdown |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 279 | mCycleView = mHeader.findViewById(R.id.cycles); |
| 280 | mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 281 | mCycleAdapter = new CycleAdapter(context); |
| 282 | mCycleSpinner.setAdapter(mCycleAdapter); |
| 283 | mCycleSpinner.setOnItemSelectedListener(mCycleListener); |
| 284 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 285 | mChart = (DataUsageChartView) mHeader.findViewById(R.id.chart); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 286 | mChart.setListener(mChartListener); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 287 | |
| 288 | { |
| 289 | // bind app detail controls |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 290 | mAppDetail = mHeader.findViewById(R.id.app_detail); |
| 291 | mAppTitle = (TextView) mAppDetail.findViewById(R.id.app_title); |
| 292 | mAppSubtitle = (TextView) mAppDetail.findViewById(R.id.app_subtitle); |
| 293 | mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 294 | |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 295 | mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 296 | mAppSettings.setOnClickListener(mAppSettingsListener); |
| 297 | |
| 298 | mAppRestrict = new CheckBox(inflater.getContext()); |
| 299 | mAppRestrict.setClickable(false); |
| 300 | mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict); |
| 301 | setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background); |
| 302 | setPreferenceSummary( |
| 303 | mAppRestrictView, R.string.data_usage_app_restrict_background_summary); |
| 304 | mAppRestrictView.setOnClickListener(mAppRestrictListener); |
| 305 | mAppSwitches.addView(mAppRestrictView); |
| 306 | } |
| 307 | |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 308 | mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary); |
Jeff Sharkey | e2afc0f | 2011-08-01 15:29:30 -0700 | [diff] [blame] | 309 | mEmpty = (TextView) mHeader.findViewById(android.R.id.empty); |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 310 | |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 311 | // only assign layout transitions once first layout is finished |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 312 | mListView.getViewTreeObserver().addOnGlobalLayoutListener(mFirstLayoutListener); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 313 | |
| 314 | mAdapter = new DataUsageAdapter(); |
| 315 | mListView.setOnItemClickListener(mListListener); |
| 316 | mListView.setAdapter(mAdapter); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 317 | |
| 318 | return view; |
| 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public void onResume() { |
| 323 | super.onResume(); |
| 324 | |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 325 | // pick default tab based on incoming intent |
| 326 | final Intent intent = getActivity().getIntent(); |
| 327 | mIntentTab = computeTabFromIntent(intent); |
| 328 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 329 | // this kicks off chain reaction which creates tabs, binds the body to |
| 330 | // selected network, and binds chart, cycles and detail list. |
| 331 | updateTabs(); |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 332 | |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 333 | // kick off background task to update stats |
| 334 | new AsyncTask<Void, Void, Void>() { |
| 335 | @Override |
| 336 | protected Void doInBackground(Void... params) { |
| 337 | try { |
| 338 | mStatsService.forceUpdate(); |
| 339 | } catch (RemoteException e) { |
| 340 | } |
| 341 | return null; |
| 342 | } |
| 343 | |
| 344 | @Override |
| 345 | protected void onPostExecute(Void result) { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 346 | if (isAdded()) { |
| 347 | updateBody(); |
| 348 | } |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 349 | } |
| 350 | }.execute(); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 353 | @Override |
| 354 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 355 | inflater.inflate(R.menu.data_usage, menu); |
| 356 | } |
| 357 | |
| 358 | @Override |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 359 | public void onPrepareOptionsMenu(Menu menu) { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 360 | final Context context = getActivity(); |
| 361 | |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 362 | mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming); |
| 363 | mMenuDataRoaming.setVisible(hasMobileRadio(context)); |
| 364 | mMenuDataRoaming.setChecked(getDataRoaming()); |
| 365 | |
| 366 | mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background); |
| 367 | mMenuRestrictBackground.setChecked(getRestrictBackground()); |
| 368 | |
| 369 | final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 370 | split4g.setVisible(hasMobile4gRadio(context)); |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 371 | split4g.setChecked(isMobilePolicySplit()); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 372 | |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 373 | final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 374 | if (hasWifiRadio(context) && hasMobileRadio(context)) { |
| 375 | showWifi.setVisible(true); |
| 376 | showWifi.setChecked(mShowWifi); |
| 377 | } else { |
| 378 | showWifi.setVisible(false); |
| 379 | mShowWifi = true; |
| 380 | } |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 381 | |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 382 | final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet); |
| 383 | if (hasEthernet(context) && hasMobileRadio(context)) { |
| 384 | showEthernet.setVisible(true); |
| 385 | showEthernet.setChecked(mShowEthernet); |
| 386 | } else { |
| 387 | showEthernet.setVisible(false); |
| 388 | mShowEthernet = true; |
| 389 | } |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 390 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 391 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 392 | @Override |
| 393 | public boolean onOptionsItemSelected(MenuItem item) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 394 | switch (item.getItemId()) { |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 395 | case R.id.data_usage_menu_roaming: { |
| 396 | final boolean dataRoaming = !item.isChecked(); |
| 397 | if (dataRoaming) { |
| 398 | ConfirmDataRoamingFragment.show(this); |
| 399 | } else { |
| 400 | // no confirmation to disable roaming |
| 401 | setDataRoaming(false); |
| 402 | } |
| 403 | return true; |
| 404 | } |
| 405 | case R.id.data_usage_menu_restrict_background: { |
| 406 | final boolean restrictBackground = !item.isChecked(); |
| 407 | if (restrictBackground) { |
| 408 | ConfirmRestrictFragment.show(this); |
| 409 | } else { |
| 410 | // no confirmation to drop restriction |
| 411 | setRestrictBackground(false); |
| 412 | } |
| 413 | return true; |
| 414 | } |
| 415 | case R.id.data_usage_menu_split_4g: { |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 416 | final boolean mobileSplit = !item.isChecked(); |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 417 | setMobilePolicySplit(mobileSplit); |
| 418 | item.setChecked(isMobilePolicySplit()); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 419 | updateTabs(); |
| 420 | return true; |
| 421 | } |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 422 | case R.id.data_usage_menu_show_wifi: { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 423 | mShowWifi = !item.isChecked(); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 424 | mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply(); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 425 | item.setChecked(mShowWifi); |
| 426 | updateTabs(); |
| 427 | return true; |
| 428 | } |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 429 | case R.id.data_usage_menu_show_ethernet: { |
| 430 | mShowEthernet = !item.isChecked(); |
| 431 | mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply(); |
| 432 | item.setChecked(mShowEthernet); |
| 433 | updateTabs(); |
| 434 | return true; |
| 435 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 436 | } |
| 437 | return false; |
| 438 | } |
| 439 | |
Jeff Sharkey | 94a9095 | 2011-06-13 22:31:09 -0700 | [diff] [blame] | 440 | @Override |
| 441 | public void onDestroyView() { |
| 442 | super.onDestroyView(); |
| 443 | |
| 444 | mDataEnabledView = null; |
| 445 | mDisableAtLimitView = null; |
| 446 | } |
| 447 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 448 | /** |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 449 | * Listener to setup {@link LayoutTransition} after first layout pass. |
| 450 | */ |
| 451 | private OnGlobalLayoutListener mFirstLayoutListener = new OnGlobalLayoutListener() { |
| 452 | /** {@inheritDoc} */ |
| 453 | public void onGlobalLayout() { |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 454 | mListView.getViewTreeObserver().removeGlobalOnLayoutListener(mFirstLayoutListener); |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 455 | |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 456 | mTabsContainer.setLayoutTransition(buildLayoutTransition()); |
| 457 | mHeader.setLayoutTransition(buildLayoutTransition()); |
| 458 | mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition()); |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 459 | |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 460 | final LayoutTransition chartTransition = buildLayoutTransition(); |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 461 | chartTransition.setStartDelay(LayoutTransition.APPEARING, 0); |
| 462 | chartTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0); |
| 463 | mChart.setLayoutTransition(chartTransition); |
| 464 | } |
| 465 | }; |
| 466 | |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 467 | private static LayoutTransition buildLayoutTransition() { |
| 468 | final LayoutTransition transition = new LayoutTransition(); |
| 469 | if (TEST_ANIM) { |
| 470 | transition.setDuration(1500); |
| 471 | } |
| 472 | transition.setAnimateParentHierarchy(false); |
| 473 | return transition; |
| 474 | } |
| 475 | |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 476 | /** |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 477 | * Rebuild all tabs based on {@link NetworkPolicyEditor} and |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 478 | * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects |
| 479 | * first tab, and kicks off a full rebind of body contents. |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 480 | */ |
| 481 | private void updateTabs() { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 482 | final Context context = getActivity(); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 483 | mTabHost.clearAllTabs(); |
| 484 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 485 | final boolean mobileSplit = isMobilePolicySplit(); |
| 486 | if (mobileSplit && hasMobile4gRadio(context)) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 487 | mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g)); |
| 488 | mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g)); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 489 | } else if (hasMobileRadio(context)) { |
| 490 | mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile)); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 491 | } |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 492 | if (mShowWifi && hasWifiRadio(context)) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 493 | mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi)); |
| 494 | } |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 495 | if (mShowEthernet && hasEthernet(context)) { |
| 496 | mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet)); |
| 497 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 498 | |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 499 | final boolean multipleTabs = mTabWidget.getTabCount() > 1; |
| 500 | mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE); |
| 501 | if (mIntentTab != null) { |
| 502 | if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) { |
| 503 | updateBody(); |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 504 | } else { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 505 | mTabHost.setCurrentTabByTag(mIntentTab); |
| 506 | } |
| 507 | mIntentTab = null; |
| 508 | } else { |
| 509 | if (mTabHost.getCurrentTab() == 0) { |
| 510 | updateBody(); |
| 511 | } else { |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 512 | mTabHost.setCurrentTab(0); |
| 513 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 517 | /** |
| 518 | * Factory that provide empty {@link View} to make {@link TabHost} happy. |
| 519 | */ |
| 520 | private TabContentFactory mEmptyTabContent = new TabContentFactory() { |
| 521 | /** {@inheritDoc} */ |
| 522 | public View createTabContent(String tag) { |
| 523 | return new View(mTabHost.getContext()); |
| 524 | } |
| 525 | }; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 526 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 527 | /** |
| 528 | * Build {@link TabSpec} with thin indicator, and empty content. |
| 529 | */ |
| 530 | private TabSpec buildTabSpec(String tag, int titleRes) { |
| 531 | final LayoutInflater inflater = LayoutInflater.from(mTabWidget.getContext()); |
| 532 | final View indicator = inflater.inflate( |
| 533 | R.layout.tab_indicator_thin_holo, mTabWidget, false); |
| 534 | final TextView title = (TextView) indicator.findViewById(android.R.id.title); |
| 535 | title.setText(titleRes); |
| 536 | return mTabHost.newTabSpec(tag).setIndicator(indicator).setContent(mEmptyTabContent); |
| 537 | } |
| 538 | |
| 539 | private OnTabChangeListener mTabListener = new OnTabChangeListener() { |
| 540 | /** {@inheritDoc} */ |
| 541 | public void onTabChanged(String tabId) { |
| 542 | // user changed tab; update body |
| 543 | updateBody(); |
| 544 | } |
| 545 | }; |
| 546 | |
| 547 | /** |
| 548 | * Update body content based on current tab. Loads |
| 549 | * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and |
| 550 | * binds them to visible controls. |
| 551 | */ |
| 552 | private void updateBody() { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 553 | mBinding = true; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 554 | |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 555 | final Context context = getActivity(); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 556 | final String currentTab = mTabHost.getCurrentTabTag(); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 557 | |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 558 | if (currentTab == null) { |
| 559 | Log.w(TAG, "no tab selected; hiding body"); |
| 560 | mListView.setVisibility(View.GONE); |
| 561 | return; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 562 | } else { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 563 | mListView.setVisibility(View.VISIBLE); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 564 | } |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 565 | |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 566 | final boolean tabChanged = !currentTab.equals(mCurrentTab); |
| 567 | mCurrentTab = currentTab; |
| 568 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 569 | if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab); |
| 570 | |
| 571 | if (TAB_WIFI.equals(currentTab)) { |
| 572 | // wifi doesn't have any controls |
| 573 | mDataEnabledView.setVisibility(View.GONE); |
| 574 | mDisableAtLimitView.setVisibility(View.GONE); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 575 | mTemplate = buildTemplateWifi(); |
| 576 | |
| 577 | } else if (TAB_ETHERNET.equals(currentTab)) { |
| 578 | // ethernet doesn't have any controls |
| 579 | mDataEnabledView.setVisibility(View.GONE); |
| 580 | mDisableAtLimitView.setVisibility(View.GONE); |
| 581 | mTemplate = buildTemplateEthernet(); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 582 | |
| 583 | } else { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 584 | mDataEnabledView.setVisibility(View.VISIBLE); |
| 585 | mDisableAtLimitView.setVisibility(View.VISIBLE); |
| 586 | } |
| 587 | |
| 588 | if (TAB_MOBILE.equals(currentTab)) { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 589 | setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile); |
| 590 | setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit); |
| 591 | mDataEnabled.setChecked(mConnService.getMobileDataEnabled()); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 592 | mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context)); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 593 | |
| 594 | } else if (TAB_3G.equals(currentTab)) { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 595 | setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g); |
| 596 | setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit); |
| 597 | // TODO: bind mDataEnabled to 3G radio state |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 598 | mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context)); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 599 | |
| 600 | } else if (TAB_4G.equals(currentTab)) { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 601 | setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g); |
| 602 | setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit); |
| 603 | // TODO: bind mDataEnabled to 4G radio state |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 604 | mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context)); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 607 | try { |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 608 | // load stats for current template |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 609 | mHistory = mStatsService.getHistoryForNetwork(mTemplate); |
| 610 | } catch (RemoteException e) { |
| 611 | // since we can't do much without policy or history, and we don't |
| 612 | // want to leave with half-baked UI, we bail hard. |
| 613 | throw new RuntimeException("problem reading network policy or stats", e); |
| 614 | } |
| 615 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 616 | // bind chart to historical stats |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 617 | mChart.bindNetworkStats(mHistory); |
| 618 | |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 619 | // only update policy when switching tabs |
| 620 | updatePolicy(tabChanged); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 621 | updateAppDetail(); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 622 | |
| 623 | // force scroll to top of body |
| 624 | mListView.smoothScrollToPosition(0); |
| 625 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 626 | mBinding = false; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 629 | private boolean isAppDetailMode() { |
| 630 | return mUid != UID_NONE; |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * Update UID details panels to match {@link #mUid}, showing or hiding them |
| 635 | * depending on {@link #isAppDetailMode()}. |
| 636 | */ |
| 637 | private void updateAppDetail() { |
| 638 | if (isAppDetailMode()) { |
| 639 | mAppDetail.setVisibility(View.VISIBLE); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 640 | mCycleAdapter.setChangeVisible(false); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 641 | } else { |
| 642 | mAppDetail.setVisibility(View.GONE); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 643 | mCycleAdapter.setChangeVisible(true); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 644 | |
| 645 | // hide detail stats when not in detail mode |
| 646 | mChart.bindDetailNetworkStats(null); |
| 647 | return; |
| 648 | } |
| 649 | |
| 650 | // remove warning/limit sweeps while in detail mode |
| 651 | mChart.bindNetworkPolicy(null); |
| 652 | |
| 653 | final PackageManager pm = getActivity().getPackageManager(); |
| 654 | mAppTitle.setText(pm.getNameForUid(mUid)); |
| 655 | |
| 656 | // enable settings button when package provides it |
| 657 | // TODO: target torwards entire UID instead of just first package |
| 658 | final String[] packageNames = pm.getPackagesForUid(mUid); |
| 659 | if (packageNames != null && packageNames.length > 0) { |
| 660 | mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE); |
| 661 | mAppSettingsIntent.setPackage(packageNames[0]); |
| 662 | mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT); |
| 663 | |
| 664 | final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null; |
| 665 | mAppSettings.setEnabled(matchFound); |
| 666 | |
| 667 | } else { |
| 668 | mAppSettingsIntent = null; |
| 669 | mAppSettings.setEnabled(false); |
| 670 | } |
| 671 | |
| 672 | try { |
| 673 | // load stats for current uid and template |
| 674 | // TODO: read template from extras |
| 675 | mDetailHistory = mStatsService.getHistoryForUid(mTemplate, mUid, NetworkStats.TAG_NONE); |
| 676 | } catch (RemoteException e) { |
| 677 | // since we can't do much without history, and we don't want to |
| 678 | // leave with half-baked UI, we bail hard. |
| 679 | throw new RuntimeException("problem reading network stats", e); |
| 680 | } |
| 681 | |
| 682 | // bind chart to historical stats |
| 683 | mChart.bindDetailNetworkStats(mDetailHistory); |
| 684 | |
| 685 | updateDetailData(); |
| 686 | |
| 687 | final Context context = getActivity(); |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 688 | if (NetworkPolicyManager.isUidValidForPolicy(context, mUid) && !getRestrictBackground()) { |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 689 | mAppRestrictView.setVisibility(View.VISIBLE); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 690 | mAppRestrict.setChecked(getAppRestrictBackground()); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 691 | |
| 692 | } else { |
| 693 | mAppRestrictView.setVisibility(View.GONE); |
| 694 | } |
| 695 | |
| 696 | } |
| 697 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 698 | private void setPolicyCycleDay(int cycleDay) { |
| 699 | if (LOGD) Log.d(TAG, "setPolicyCycleDay()"); |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 700 | mPolicyEditor.setPolicyCycleDay(mTemplate, cycleDay); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 701 | updatePolicy(true); |
| 702 | } |
| 703 | |
| 704 | private void setPolicyWarningBytes(long warningBytes) { |
| 705 | if (LOGD) Log.d(TAG, "setPolicyWarningBytes()"); |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 706 | mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 707 | updatePolicy(false); |
| 708 | } |
| 709 | |
| 710 | private void setPolicyLimitBytes(long limitBytes) { |
| 711 | if (LOGD) Log.d(TAG, "setPolicyLimitBytes()"); |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 712 | mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 713 | updatePolicy(false); |
| 714 | } |
| 715 | |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 716 | private boolean getDataRoaming() { |
| 717 | final ContentResolver resolver = getActivity().getContentResolver(); |
| 718 | return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0; |
| 719 | } |
| 720 | |
| 721 | private void setDataRoaming(boolean enabled) { |
| 722 | // TODO: teach telephony DataConnectionTracker to watch and apply |
| 723 | // updates when changed. |
| 724 | final ContentResolver resolver = getActivity().getContentResolver(); |
| 725 | Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0); |
| 726 | mMenuDataRoaming.setChecked(enabled); |
| 727 | } |
| 728 | |
| 729 | private boolean getRestrictBackground() { |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 730 | try { |
| 731 | return mPolicyService.getRestrictBackground(); |
| 732 | } catch (RemoteException e) { |
| 733 | Log.w(TAG, "problem talking with policy service: " + e); |
| 734 | return false; |
| 735 | } |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | private void setRestrictBackground(boolean restrictBackground) { |
| 739 | if (LOGD) Log.d(TAG, "setRestrictBackground()"); |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 740 | try { |
| 741 | mPolicyService.setRestrictBackground(restrictBackground); |
| 742 | mMenuRestrictBackground.setChecked(restrictBackground); |
| 743 | } catch (RemoteException e) { |
| 744 | Log.w(TAG, "problem talking with policy service: " + e); |
| 745 | } |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | private boolean getAppRestrictBackground() { |
| 749 | final int uidPolicy; |
| 750 | try { |
| 751 | uidPolicy = mPolicyService.getUidPolicy(mUid); |
| 752 | } catch (RemoteException e) { |
| 753 | // since we can't do much without policy, we bail hard. |
| 754 | throw new RuntimeException("problem reading network policy", e); |
| 755 | } |
| 756 | |
| 757 | return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0; |
| 758 | } |
| 759 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 760 | private void setAppRestrictBackground(boolean restrictBackground) { |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 761 | if (LOGD) Log.d(TAG, "setAppRestrictBackground()"); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 762 | try { |
| 763 | mPolicyService.setUidPolicy( |
| 764 | mUid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE); |
| 765 | } catch (RemoteException e) { |
| 766 | throw new RuntimeException("unable to save policy", e); |
| 767 | } |
| 768 | |
| 769 | mAppRestrict.setChecked(restrictBackground); |
| 770 | } |
| 771 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 772 | /** |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 773 | * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for |
| 774 | * current {@link #mTemplate}. |
| 775 | */ |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 776 | private void updatePolicy(boolean refreshCycle) { |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 777 | if (isAppDetailMode()) { |
| 778 | mNetworkSwitches.setVisibility(View.GONE); |
| 779 | // we fall through to update cycle list for detail mode |
| 780 | } else { |
| 781 | mNetworkSwitches.setVisibility(View.VISIBLE); |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 782 | |
| 783 | // when heading back to summary without cycle refresh, kick details |
| 784 | // update to repopulate list. |
| 785 | if (!refreshCycle) { |
| 786 | updateDetailData(); |
| 787 | } |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 788 | } |
| 789 | |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 790 | final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate); |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 791 | |
| 792 | // reflect policy limit in checkbox |
| 793 | mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED); |
| 794 | mChart.bindNetworkPolicy(policy); |
| 795 | |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 796 | if (refreshCycle) { |
| 797 | // generate cycle list based on policy and available history |
| 798 | updateCycleList(policy); |
| 799 | } |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | /** |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 803 | * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay} |
| 804 | * and available {@link NetworkStatsHistory} data. Always selects the newest |
| 805 | * item, updating the inspection range on {@link #mChart}. |
| 806 | */ |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 807 | private void updateCycleList(NetworkPolicy policy) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 808 | mCycleAdapter.clear(); |
| 809 | |
| 810 | final Context context = mCycleSpinner.getContext(); |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 811 | long historyStart = mHistory.getStart(); |
| 812 | long historyEnd = mHistory.getEnd(); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 813 | |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 814 | if (historyStart == Long.MAX_VALUE || historyEnd == Long.MIN_VALUE) { |
| 815 | historyStart = System.currentTimeMillis(); |
| 816 | historyEnd = System.currentTimeMillis(); |
| 817 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 818 | |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 819 | boolean hasCycles = false; |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 820 | if (policy != null) { |
| 821 | // find the next cycle boundary |
| 822 | long cycleEnd = computeNextCycleBoundary(historyEnd, policy); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 823 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 824 | int guardCount = 0; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 825 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 826 | // walk backwards, generating all valid cycle ranges |
| 827 | while (cycleEnd > historyStart) { |
| 828 | final long cycleStart = computeLastCycleBoundary(cycleEnd, policy); |
| 829 | Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs=" |
| 830 | + historyStart); |
| 831 | mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd)); |
| 832 | cycleEnd = cycleStart; |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 833 | hasCycles = true; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 834 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 835 | // TODO: remove this guard once we have better testing |
| 836 | if (guardCount++ > 50) { |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 837 | Log.wtf(TAG, "stuck generating ranges for historyStart=" + historyStart |
| 838 | + ", historyEnd=" + historyEnd + " and policy=" + policy); |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 839 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 840 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 841 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 842 | // one last cycle entry to modify policy cycle day |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 843 | mCycleAdapter.setChangePossible(true); |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 844 | } |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 845 | |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 846 | if (!hasCycles) { |
| 847 | // no valid cycles; show all data |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 848 | // TODO: offer simple ranges like "last week" etc |
| 849 | mCycleAdapter.add(new CycleItem(context, historyStart, historyEnd)); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 850 | mCycleAdapter.setChangePossible(false); |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 851 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 852 | |
| 853 | // force pick the current cycle (first item) |
| 854 | mCycleSpinner.setSelection(0); |
| 855 | mCycleListener.onItemSelected(mCycleSpinner, null, 0, 0); |
| 856 | } |
| 857 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 858 | private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 859 | /** {@inheritDoc} */ |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 860 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 861 | if (mBinding) return; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 862 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 863 | final boolean dataEnabled = isChecked; |
| 864 | mDataEnabled.setChecked(dataEnabled); |
| 865 | |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 866 | final String currentTab = mCurrentTab; |
| 867 | if (TAB_MOBILE.equals(currentTab)) { |
| 868 | mConnService.setMobileDataEnabled(dataEnabled); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 869 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 870 | } |
| 871 | }; |
| 872 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 873 | private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 874 | /** {@inheritDoc} */ |
| 875 | public void onClick(View v) { |
| 876 | final boolean disableAtLimit = !mDisableAtLimit.isChecked(); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 877 | if (disableAtLimit) { |
| 878 | // enabling limit; show confirmation dialog which eventually |
| 879 | // calls setPolicyLimitBytes() once user confirms. |
| 880 | ConfirmLimitFragment.show(DataUsageSummary.this); |
| 881 | } else { |
| 882 | setPolicyLimitBytes(LIMIT_DISABLED); |
| 883 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 884 | } |
| 885 | }; |
| 886 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 887 | private View.OnClickListener mAppRestrictListener = new View.OnClickListener() { |
| 888 | /** {@inheritDoc} */ |
| 889 | public void onClick(View v) { |
| 890 | final boolean restrictBackground = !mAppRestrict.isChecked(); |
| 891 | |
| 892 | if (restrictBackground) { |
| 893 | // enabling restriction; show confirmation dialog which |
| 894 | // eventually calls setRestrictBackground() once user confirms. |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 895 | ConfirmAppRestrictFragment.show(DataUsageSummary.this); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 896 | } else { |
| 897 | setAppRestrictBackground(false); |
| 898 | } |
| 899 | } |
| 900 | }; |
| 901 | |
| 902 | private OnClickListener mAppSettingsListener = new OnClickListener() { |
| 903 | /** {@inheritDoc} */ |
| 904 | public void onClick(View v) { |
| 905 | // TODO: target torwards entire UID instead of just first package |
| 906 | startActivity(mAppSettingsIntent); |
| 907 | } |
| 908 | }; |
| 909 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 910 | private OnItemClickListener mListListener = new OnItemClickListener() { |
| 911 | /** {@inheritDoc} */ |
| 912 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 913 | final AppUsageItem app = (AppUsageItem) parent.getItemAtPosition(position); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 914 | AppDetailsFragment.show(DataUsageSummary.this, app.uid); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 915 | } |
| 916 | }; |
| 917 | |
| 918 | private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() { |
| 919 | /** {@inheritDoc} */ |
| 920 | public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
| 921 | final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position); |
| 922 | if (cycle instanceof CycleChangeItem) { |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 923 | // show cycle editor; will eventually call setPolicyCycleDay() |
| 924 | // when user finishes editing. |
| 925 | CycleEditorFragment.show(DataUsageSummary.this); |
| 926 | |
| 927 | // reset spinner to something other than "change cycle..." |
| 928 | mCycleSpinner.setSelection(0); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 929 | |
| 930 | } else { |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 931 | if (LOGD) { |
| 932 | Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end=" |
| 933 | + cycle.end + "]"); |
| 934 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 935 | |
| 936 | // update chart to show selected cycle, and update detail data |
| 937 | // to match updated sweep bounds. |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 938 | mChart.setVisibleRange(cycle.start, cycle.end); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 939 | |
| 940 | updateDetailData(); |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | /** {@inheritDoc} */ |
| 945 | public void onNothingSelected(AdapterView<?> parent) { |
| 946 | // ignored |
| 947 | } |
| 948 | }; |
| 949 | |
| 950 | /** |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 951 | * Update details based on {@link #mChart} inspection range depending on |
| 952 | * current mode. In network mode, updates {@link #mAdapter} with sorted list |
| 953 | * of applications data usage, and when {@link #isAppDetailMode()} update |
| 954 | * app details. |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 955 | */ |
| 956 | private void updateDetailData() { |
| 957 | if (LOGD) Log.d(TAG, "updateDetailData()"); |
| 958 | |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 959 | final long start = mChart.getInspectStart(); |
| 960 | final long end = mChart.getInspectEnd(); |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 961 | final long now = System.currentTimeMillis(); |
| 962 | |
| 963 | final Context context = getActivity(); |
| 964 | final NetworkStatsHistory.Entry entry; |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 965 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 966 | if (isAppDetailMode()) { |
| 967 | if (mDetailHistory != null) { |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 968 | entry = mDetailHistory.getValues(start, end, now, null); |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 969 | |
| 970 | mAppSubtitle.setText( |
| 971 | getString(R.string.data_usage_received_sent, |
| 972 | Formatter.formatFileSize(context, entry.rxBytes), |
| 973 | Formatter.formatFileSize(context, entry.txBytes))); |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 974 | } else { |
| 975 | entry = null; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 976 | } |
| 977 | |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 978 | getLoaderManager().destroyLoader(LOADER_SUMMARY); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 979 | |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 980 | } else { |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 981 | entry = mHistory.getValues(start, end, now, null); |
| 982 | |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 983 | // kick off loader for detailed stats |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 984 | getLoaderManager().restartLoader(LOADER_SUMMARY, |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 985 | SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryForAllUid); |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 986 | |
| 987 | } |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 988 | |
| 989 | final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0; |
| 990 | final String totalPhrase = Formatter.formatFileSize(context, totalBytes); |
Jeff Sharkey | e2afc0f | 2011-08-01 15:29:30 -0700 | [diff] [blame] | 991 | final String rangePhrase = formatDateRange(context, start, end, null); |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 992 | |
| 993 | mUsageSummary.setText( |
| 994 | getString(R.string.data_usage_total_during_range, totalPhrase, rangePhrase)); |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | private final LoaderCallbacks<NetworkStats> mSummaryForAllUid = new LoaderCallbacks< |
| 998 | NetworkStats>() { |
| 999 | /** {@inheritDoc} */ |
| 1000 | public Loader<NetworkStats> onCreateLoader(int id, Bundle args) { |
| 1001 | return new SummaryForAllUidLoader(getActivity(), mStatsService, args); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1004 | /** {@inheritDoc} */ |
| 1005 | public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) { |
| 1006 | mAdapter.bindStats(data); |
Jeff Sharkey | e2afc0f | 2011-08-01 15:29:30 -0700 | [diff] [blame] | 1007 | updateEmptyVisible(); |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1008 | } |
Jeff Sharkey | aa5260e | 2011-06-14 23:21:59 -0700 | [diff] [blame] | 1009 | |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1010 | /** {@inheritDoc} */ |
| 1011 | public void onLoaderReset(Loader<NetworkStats> loader) { |
| 1012 | mAdapter.bindStats(null); |
Jeff Sharkey | e2afc0f | 2011-08-01 15:29:30 -0700 | [diff] [blame] | 1013 | updateEmptyVisible(); |
| 1014 | } |
| 1015 | |
| 1016 | private void updateEmptyVisible() { |
| 1017 | final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode(); |
| 1018 | mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE); |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1019 | } |
| 1020 | }; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1021 | |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 1022 | private boolean isMobilePolicySplit() { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 1023 | final Context context = getActivity(); |
| 1024 | if (hasMobileRadio(context)) { |
| 1025 | final String subscriberId = getActiveSubscriberId(context); |
| 1026 | return mPolicyEditor.isMobilePolicySplit(subscriberId); |
| 1027 | } else { |
| 1028 | return false; |
| 1029 | } |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | private void setMobilePolicySplit(boolean split) { |
| 1033 | final String subscriberId = getActiveSubscriberId(getActivity()); |
| 1034 | mPolicyEditor.setMobilePolicySplit(subscriberId, split); |
| 1035 | } |
| 1036 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 1037 | private static String getActiveSubscriberId(Context context) { |
| 1038 | final TelephonyManager telephony = (TelephonyManager) context.getSystemService( |
| 1039 | Context.TELEPHONY_SERVICE); |
| 1040 | return telephony.getSubscriberId(); |
| 1041 | } |
| 1042 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1043 | private DataUsageChartListener mChartListener = new DataUsageChartListener() { |
| 1044 | /** {@inheritDoc} */ |
| 1045 | public void onInspectRangeChanged() { |
| 1046 | if (LOGD) Log.d(TAG, "onInspectRangeChanged()"); |
| 1047 | updateDetailData(); |
| 1048 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1049 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1050 | /** {@inheritDoc} */ |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 1051 | public void onWarningChanged() { |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1052 | setPolicyWarningBytes(mChart.getWarningBytes()); |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 1053 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1054 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 1055 | /** {@inheritDoc} */ |
| 1056 | public void onLimitChanged() { |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1057 | setPolicyLimitBytes(mChart.getLimitBytes()); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1058 | } |
| 1059 | }; |
| 1060 | |
| 1061 | |
| 1062 | /** |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1063 | * List item that reflects a specific data usage cycle. |
| 1064 | */ |
| 1065 | public static class CycleItem { |
| 1066 | public CharSequence label; |
| 1067 | public long start; |
| 1068 | public long end; |
| 1069 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1070 | CycleItem(CharSequence label) { |
| 1071 | this.label = label; |
| 1072 | } |
| 1073 | |
| 1074 | public CycleItem(Context context, long start, long end) { |
Jeff Sharkey | e2afc0f | 2011-08-01 15:29:30 -0700 | [diff] [blame] | 1075 | this.label = formatDateRange(context, start, end, Time.TIMEZONE_UTC); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1076 | this.start = start; |
| 1077 | this.end = end; |
| 1078 | } |
| 1079 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1080 | @Override |
| 1081 | public String toString() { |
| 1082 | return label.toString(); |
| 1083 | } |
| 1084 | } |
| 1085 | |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1086 | private static final StringBuilder sBuilder = new StringBuilder(50); |
| 1087 | private static final java.util.Formatter sFormatter = new java.util.Formatter( |
| 1088 | sBuilder, Locale.getDefault()); |
| 1089 | |
Jeff Sharkey | e2afc0f | 2011-08-01 15:29:30 -0700 | [diff] [blame] | 1090 | private static String formatDateRange(Context context, long start, long end, String timezone) { |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1091 | synchronized (sBuilder) { |
| 1092 | int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_MONTH; |
| 1093 | if (Time.getJulianDay(start, 0) == Time.getJulianDay(end, 0)) { |
| 1094 | // when times are on same day, include time detail |
| 1095 | flags |= DateUtils.FORMAT_SHOW_TIME; |
| 1096 | } |
| 1097 | |
| 1098 | sBuilder.setLength(0); |
Jeff Sharkey | e2afc0f | 2011-08-01 15:29:30 -0700 | [diff] [blame] | 1099 | return DateUtils |
| 1100 | .formatDateRange(context, sFormatter, start, end, flags, timezone).toString(); |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1101 | } |
| 1102 | } |
| 1103 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1104 | /** |
| 1105 | * Special-case data usage cycle that triggers dialog to change |
| 1106 | * {@link NetworkPolicy#cycleDay}. |
| 1107 | */ |
| 1108 | public static class CycleChangeItem extends CycleItem { |
| 1109 | public CycleChangeItem(Context context) { |
| 1110 | super(context.getString(R.string.data_usage_change_cycle)); |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | public static class CycleAdapter extends ArrayAdapter<CycleItem> { |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1115 | private boolean mChangePossible = false; |
| 1116 | private boolean mChangeVisible = false; |
| 1117 | |
| 1118 | private final CycleChangeItem mChangeItem; |
| 1119 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1120 | public CycleAdapter(Context context) { |
| 1121 | super(context, android.R.layout.simple_spinner_item); |
| 1122 | setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1123 | mChangeItem = new CycleChangeItem(context); |
| 1124 | } |
| 1125 | |
| 1126 | public void setChangePossible(boolean possible) { |
| 1127 | mChangePossible = possible; |
| 1128 | updateChange(); |
| 1129 | } |
| 1130 | |
| 1131 | public void setChangeVisible(boolean visible) { |
| 1132 | mChangeVisible = visible; |
| 1133 | updateChange(); |
| 1134 | } |
| 1135 | |
| 1136 | private void updateChange() { |
| 1137 | remove(mChangeItem); |
| 1138 | if (mChangePossible && mChangeVisible) { |
| 1139 | add(mChangeItem); |
| 1140 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1141 | } |
| 1142 | } |
| 1143 | |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 1144 | private static class AppUsageItem implements Comparable<AppUsageItem> { |
| 1145 | public int uid; |
| 1146 | public long total; |
| 1147 | |
| 1148 | /** {@inheritDoc} */ |
| 1149 | public int compareTo(AppUsageItem another) { |
| 1150 | return Long.compare(another.total, total); |
| 1151 | } |
| 1152 | } |
| 1153 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1154 | /** |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1155 | * Adapter of applications, sorted by total usage descending. |
| 1156 | */ |
| 1157 | public static class DataUsageAdapter extends BaseAdapter { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1158 | private ArrayList<AppUsageItem> mItems = Lists.newArrayList(); |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 1159 | private long mLargest; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1160 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1161 | /** |
| 1162 | * Bind the given {@link NetworkStats}, or {@code null} to clear list. |
| 1163 | */ |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1164 | public void bindStats(NetworkStats stats) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1165 | mItems.clear(); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1166 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1167 | if (stats != null) { |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1168 | final AppUsageItem systemItem = new AppUsageItem(); |
| 1169 | systemItem.uid = android.os.Process.SYSTEM_UID; |
| 1170 | |
Jeff Sharkey | ebae659 | 2011-07-12 13:53:11 -0700 | [diff] [blame] | 1171 | NetworkStats.Entry entry = null; |
| 1172 | for (int i = 0; i < stats.size(); i++) { |
| 1173 | entry = stats.getValues(i, entry); |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1174 | |
| 1175 | final boolean isApp = entry.uid >= android.os.Process.FIRST_APPLICATION_UID |
| 1176 | && entry.uid <= android.os.Process.LAST_APPLICATION_UID; |
| 1177 | if (isApp || entry.uid == TrafficStats.UID_REMOVED) { |
| 1178 | final AppUsageItem item = new AppUsageItem(); |
| 1179 | item.uid = entry.uid; |
| 1180 | item.total = entry.rxBytes + entry.txBytes; |
| 1181 | mItems.add(item); |
| 1182 | } else { |
| 1183 | systemItem.total += entry.rxBytes + entry.txBytes; |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | if (systemItem.total > 0) { |
| 1188 | mItems.add(systemItem); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1189 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1190 | } |
| 1191 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1192 | Collections.sort(mItems); |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 1193 | mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1194 | notifyDataSetChanged(); |
| 1195 | } |
| 1196 | |
| 1197 | @Override |
| 1198 | public int getCount() { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1199 | return mItems.size(); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | @Override |
| 1203 | public Object getItem(int position) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1204 | return mItems.get(position); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | @Override |
| 1208 | public long getItemId(int position) { |
Jeff Sharkey | e2afc0f | 2011-08-01 15:29:30 -0700 | [diff] [blame] | 1209 | return mItems.get(position).uid; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | @Override |
| 1213 | public View getView(int position, View convertView, ViewGroup parent) { |
| 1214 | if (convertView == null) { |
| 1215 | convertView = LayoutInflater.from(parent.getContext()).inflate( |
Jeff Sharkey | 52c3f44 | 2011-06-23 00:39:38 -0700 | [diff] [blame] | 1216 | R.layout.data_usage_item, parent, false); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | final Context context = parent.getContext(); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1220 | |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 1221 | final TextView title = (TextView) convertView.findViewById(android.R.id.title); |
| 1222 | final TextView summary = (TextView) convertView.findViewById(android.R.id.summary); |
| 1223 | final ProgressBar progress = (ProgressBar) convertView.findViewById( |
| 1224 | android.R.id.progress); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1225 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1226 | final AppUsageItem item = mItems.get(position); |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 1227 | title.setText(resolveLabelForUid(context, item.uid)); |
| 1228 | summary.setText(Formatter.formatFileSize(context, item.total)); |
| 1229 | |
| 1230 | final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0; |
| 1231 | progress.setProgress(percentTotal); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1232 | |
| 1233 | return convertView; |
| 1234 | } |
| 1235 | |
| 1236 | } |
| 1237 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1238 | /** |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1239 | * Empty {@link Fragment} that controls display of UID details in |
| 1240 | * {@link DataUsageSummary}. |
| 1241 | */ |
| 1242 | public static class AppDetailsFragment extends Fragment { |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 1243 | private static final String EXTRA_UID = "uid"; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1244 | |
| 1245 | public static void show(DataUsageSummary parent, int uid) { |
| 1246 | final Bundle args = new Bundle(); |
| 1247 | args.putInt(EXTRA_UID, uid); |
| 1248 | |
| 1249 | final AppDetailsFragment fragment = new AppDetailsFragment(); |
| 1250 | fragment.setArguments(args); |
| 1251 | fragment.setTargetFragment(parent, 0); |
| 1252 | |
| 1253 | final FragmentTransaction ft = parent.getFragmentManager().beginTransaction(); |
| 1254 | ft.add(fragment, TAG_APP_DETAILS); |
| 1255 | ft.addToBackStack(TAG_APP_DETAILS); |
| 1256 | ft.commit(); |
| 1257 | } |
| 1258 | |
| 1259 | @Override |
| 1260 | public void onStart() { |
| 1261 | super.onStart(); |
| 1262 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1263 | target.mUid = getArguments().getInt(EXTRA_UID); |
| 1264 | target.updateBody(); |
| 1265 | } |
| 1266 | |
| 1267 | @Override |
| 1268 | public void onStop() { |
| 1269 | super.onStop(); |
| 1270 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1271 | target.mUid = UID_NONE; |
| 1272 | target.updateBody(); |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | /** |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1277 | * Dialog to request user confirmation before setting |
| 1278 | * {@link NetworkPolicy#limitBytes}. |
| 1279 | */ |
| 1280 | public static class ConfirmLimitFragment extends DialogFragment { |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 1281 | private static final String EXTRA_MESSAGE_ID = "messageId"; |
| 1282 | private static final String EXTRA_LIMIT_BYTES = "limitBytes"; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1283 | |
| 1284 | public static void show(DataUsageSummary parent) { |
| 1285 | final Bundle args = new Bundle(); |
| 1286 | |
| 1287 | // TODO: customize default limits based on network template |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 1288 | final String currentTab = parent.mCurrentTab; |
| 1289 | if (TAB_3G.equals(currentTab)) { |
| 1290 | args.putInt(EXTRA_MESSAGE_ID, R.string.data_usage_limit_dialog_3g); |
| 1291 | args.putLong(EXTRA_LIMIT_BYTES, 5 * GB_IN_BYTES); |
| 1292 | } else if (TAB_4G.equals(currentTab)) { |
| 1293 | args.putInt(EXTRA_MESSAGE_ID, R.string.data_usage_limit_dialog_4g); |
| 1294 | args.putLong(EXTRA_LIMIT_BYTES, 5 * GB_IN_BYTES); |
| 1295 | } else if (TAB_MOBILE.equals(currentTab)) { |
| 1296 | args.putInt(EXTRA_MESSAGE_ID, R.string.data_usage_limit_dialog_mobile); |
| 1297 | args.putLong(EXTRA_LIMIT_BYTES, 5 * GB_IN_BYTES); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | final ConfirmLimitFragment dialog = new ConfirmLimitFragment(); |
| 1301 | dialog.setArguments(args); |
| 1302 | dialog.setTargetFragment(parent, 0); |
| 1303 | dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT); |
| 1304 | } |
| 1305 | |
| 1306 | @Override |
| 1307 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1308 | final Context context = getActivity(); |
| 1309 | |
| 1310 | final int messageId = getArguments().getInt(EXTRA_MESSAGE_ID); |
| 1311 | final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES); |
| 1312 | |
| 1313 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 1314 | builder.setTitle(R.string.data_usage_limit_dialog_title); |
| 1315 | builder.setMessage(messageId); |
| 1316 | |
| 1317 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
| 1318 | public void onClick(DialogInterface dialog, int which) { |
| 1319 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1320 | if (target != null) { |
| 1321 | target.setPolicyLimitBytes(limitBytes); |
| 1322 | } |
| 1323 | } |
| 1324 | }); |
| 1325 | |
| 1326 | return builder.create(); |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | /** |
| 1331 | * Dialog to edit {@link NetworkPolicy#cycleDay}. |
| 1332 | */ |
| 1333 | public static class CycleEditorFragment extends DialogFragment { |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 1334 | private static final String EXTRA_CYCLE_DAY = "cycleDay"; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1335 | |
| 1336 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 1337 | final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1338 | final Bundle args = new Bundle(); |
| 1339 | args.putInt(CycleEditorFragment.EXTRA_CYCLE_DAY, policy.cycleDay); |
| 1340 | |
| 1341 | final CycleEditorFragment dialog = new CycleEditorFragment(); |
| 1342 | dialog.setArguments(args); |
| 1343 | dialog.setTargetFragment(parent, 0); |
| 1344 | dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR); |
| 1345 | } |
| 1346 | |
| 1347 | @Override |
| 1348 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1349 | final Context context = getActivity(); |
| 1350 | |
| 1351 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 1352 | final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext()); |
| 1353 | |
| 1354 | final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false); |
| 1355 | final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day); |
| 1356 | |
| 1357 | final int oldCycleDay = getArguments().getInt(EXTRA_CYCLE_DAY, 1); |
| 1358 | |
| 1359 | cycleDayPicker.setMinValue(1); |
| 1360 | cycleDayPicker.setMaxValue(31); |
| 1361 | cycleDayPicker.setValue(oldCycleDay); |
| 1362 | cycleDayPicker.setWrapSelectorWheel(true); |
| 1363 | |
| 1364 | builder.setTitle(R.string.data_usage_cycle_editor_title); |
| 1365 | builder.setView(view); |
| 1366 | |
| 1367 | builder.setPositiveButton(R.string.data_usage_cycle_editor_positive, |
| 1368 | new DialogInterface.OnClickListener() { |
| 1369 | public void onClick(DialogInterface dialog, int which) { |
| 1370 | final int cycleDay = cycleDayPicker.getValue(); |
| 1371 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1372 | if (target != null) { |
| 1373 | target.setPolicyCycleDay(cycleDay); |
| 1374 | } |
| 1375 | } |
| 1376 | }); |
| 1377 | |
| 1378 | return builder.create(); |
| 1379 | } |
| 1380 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1381 | |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 1382 | /** |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1383 | * Dialog to request user confirmation before setting |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1384 | * {@link Settings.Secure#DATA_ROAMING}. |
| 1385 | */ |
| 1386 | public static class ConfirmDataRoamingFragment extends DialogFragment { |
| 1387 | public static void show(DataUsageSummary parent) { |
| 1388 | final Bundle args = new Bundle(); |
| 1389 | |
| 1390 | final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment(); |
| 1391 | dialog.setTargetFragment(parent, 0); |
| 1392 | dialog.show(parent.getFragmentManager(), TAG_CONFIRM_ROAMING); |
| 1393 | } |
| 1394 | |
| 1395 | @Override |
| 1396 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1397 | final Context context = getActivity(); |
| 1398 | |
| 1399 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 1400 | builder.setTitle(R.string.roaming_reenable_title); |
| 1401 | builder.setMessage(R.string.roaming_warning); |
| 1402 | |
| 1403 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
| 1404 | public void onClick(DialogInterface dialog, int which) { |
| 1405 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1406 | if (target != null) { |
| 1407 | target.setDataRoaming(true); |
| 1408 | } |
| 1409 | } |
| 1410 | }); |
| 1411 | builder.setNegativeButton(android.R.string.cancel, null); |
| 1412 | |
| 1413 | return builder.create(); |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | /** |
| 1418 | * Dialog to request user confirmation before setting |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1419 | * {@link INetworkPolicyManager#setRestrictBackground(boolean)}. |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1420 | */ |
| 1421 | public static class ConfirmRestrictFragment extends DialogFragment { |
| 1422 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1423 | final Bundle args = new Bundle(); |
| 1424 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1425 | final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment(); |
| 1426 | dialog.setTargetFragment(parent, 0); |
| 1427 | dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT); |
| 1428 | } |
| 1429 | |
| 1430 | @Override |
| 1431 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1432 | final Context context = getActivity(); |
| 1433 | |
| 1434 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1435 | builder.setTitle(R.string.data_usage_restrict_background_title); |
| 1436 | builder.setMessage(R.string.data_usage_restrict_background); |
| 1437 | |
| 1438 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
| 1439 | public void onClick(DialogInterface dialog, int which) { |
| 1440 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1441 | if (target != null) { |
| 1442 | target.setRestrictBackground(true); |
| 1443 | } |
| 1444 | } |
| 1445 | }); |
| 1446 | builder.setNegativeButton(android.R.string.cancel, null); |
| 1447 | |
| 1448 | return builder.create(); |
| 1449 | } |
| 1450 | } |
| 1451 | |
| 1452 | /** |
| 1453 | * Dialog to request user confirmation before setting |
| 1454 | * {@link #POLICY_REJECT_METERED_BACKGROUND}. |
| 1455 | */ |
| 1456 | public static class ConfirmAppRestrictFragment extends DialogFragment { |
| 1457 | public static void show(DataUsageSummary parent) { |
| 1458 | final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment(); |
| 1459 | dialog.setTargetFragment(parent, 0); |
| 1460 | dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT); |
| 1461 | } |
| 1462 | |
| 1463 | @Override |
| 1464 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1465 | final Context context = getActivity(); |
| 1466 | |
| 1467 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1468 | builder.setTitle(R.string.data_usage_app_restrict_dialog_title); |
| 1469 | builder.setMessage(R.string.data_usage_app_restrict_dialog); |
| 1470 | |
| 1471 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
| 1472 | public void onClick(DialogInterface dialog, int which) { |
| 1473 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1474 | if (target != null) { |
| 1475 | target.setAppRestrictBackground(true); |
| 1476 | } |
| 1477 | } |
| 1478 | }); |
| 1479 | builder.setNegativeButton(android.R.string.cancel, null); |
| 1480 | |
| 1481 | return builder.create(); |
| 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | /** |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 1486 | * Compute default tab that should be selected, based on |
| 1487 | * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra. |
| 1488 | */ |
| 1489 | private static String computeTabFromIntent(Intent intent) { |
Jeff Sharkey | 271ec8a | 2011-07-20 16:59:16 -0700 | [diff] [blame] | 1490 | final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE); |
| 1491 | if (template == null) return null; |
| 1492 | |
| 1493 | switch (template.getMatchRule()) { |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 1494 | case MATCH_MOBILE_3G_LOWER: |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 1495 | return TAB_3G; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 1496 | case MATCH_MOBILE_4G: |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 1497 | return TAB_4G; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 1498 | case MATCH_MOBILE_ALL: |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 1499 | return TAB_MOBILE; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 1500 | case MATCH_WIFI: |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 1501 | return TAB_WIFI; |
| 1502 | default: |
| 1503 | return null; |
| 1504 | } |
| 1505 | } |
| 1506 | |
| 1507 | /** |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 1508 | * Resolve best descriptive label for the given UID. |
| 1509 | */ |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1510 | public static CharSequence resolveLabelForUid(Context context, int uid) { |
| 1511 | final Resources res = context.getResources(); |
| 1512 | final PackageManager pm = context.getPackageManager(); |
| 1513 | |
| 1514 | // handle special case labels |
| 1515 | switch (uid) { |
| 1516 | case android.os.Process.SYSTEM_UID: |
| 1517 | return res.getText(R.string.process_kernel_label); |
| 1518 | case TrafficStats.UID_REMOVED: |
| 1519 | return res.getText(R.string.data_usage_uninstalled_apps); |
| 1520 | } |
| 1521 | |
| 1522 | // otherwise fall back to using packagemanager labels |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 1523 | final String[] packageNames = pm.getPackagesForUid(uid); |
| 1524 | final int length = packageNames != null ? packageNames.length : 0; |
| 1525 | |
| 1526 | CharSequence label = pm.getNameForUid(uid); |
| 1527 | try { |
| 1528 | if (length == 1) { |
| 1529 | final ApplicationInfo info = pm.getApplicationInfo(packageNames[0], 0); |
| 1530 | label = info.loadLabel(pm); |
| 1531 | } else if (length > 1) { |
| 1532 | for (String packageName : packageNames) { |
| 1533 | final PackageInfo info = pm.getPackageInfo(packageName, 0); |
| 1534 | if (info.sharedUserLabel != 0) { |
| 1535 | label = pm.getText(packageName, info.sharedUserLabel, info.applicationInfo); |
| 1536 | if (!TextUtils.isEmpty(label)) { |
| 1537 | break; |
| 1538 | } |
| 1539 | } |
| 1540 | } |
| 1541 | } |
| 1542 | } catch (NameNotFoundException e) { |
| 1543 | } |
| 1544 | |
| 1545 | if (TextUtils.isEmpty(label)) { |
| 1546 | label = Integer.toString(uid); |
| 1547 | } |
| 1548 | return label; |
| 1549 | } |
| 1550 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 1551 | /** |
| 1552 | * Test if device has a mobile data radio. |
| 1553 | */ |
| 1554 | private static boolean hasMobileRadio(Context context) { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 1555 | if (TEST_RADIOS) { |
| 1556 | return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile"); |
| 1557 | } |
| 1558 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 1559 | final ConnectivityManager conn = (ConnectivityManager) context.getSystemService( |
| 1560 | Context.CONNECTIVITY_SERVICE); |
| 1561 | |
| 1562 | // mobile devices should have MOBILE network tracker regardless of |
| 1563 | // connection status. |
| 1564 | return conn.getNetworkInfo(TYPE_MOBILE) != null; |
| 1565 | } |
| 1566 | |
| 1567 | /** |
| 1568 | * Test if device has a mobile 4G data radio. |
| 1569 | */ |
| 1570 | private static boolean hasMobile4gRadio(Context context) { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 1571 | if (TEST_RADIOS) { |
| 1572 | return SystemProperties.get(TEST_RADIOS_PROP).contains("4g"); |
| 1573 | } |
| 1574 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 1575 | final ConnectivityManager conn = (ConnectivityManager) context.getSystemService( |
| 1576 | Context.CONNECTIVITY_SERVICE); |
| 1577 | final TelephonyManager telephony = (TelephonyManager) context.getSystemService( |
| 1578 | Context.TELEPHONY_SERVICE); |
| 1579 | |
| 1580 | // WiMAX devices should have WiMAX network tracker regardless of |
| 1581 | // connection status. |
| 1582 | final boolean hasWimax = conn.getNetworkInfo(TYPE_WIMAX) != null; |
| 1583 | final boolean hasLte = telephony.getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE; |
| 1584 | return hasWimax || hasLte; |
| 1585 | } |
| 1586 | |
| 1587 | /** |
| 1588 | * Test if device has a Wi-Fi data radio. |
| 1589 | */ |
| 1590 | private static boolean hasWifiRadio(Context context) { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 1591 | if (TEST_RADIOS) { |
| 1592 | return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi"); |
| 1593 | } |
| 1594 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 1595 | return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI); |
| 1596 | } |
| 1597 | |
| 1598 | /** |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 1599 | * Test if device has an ethernet network connection. |
| 1600 | */ |
| 1601 | private static boolean hasEthernet(Context context) { |
| 1602 | if (TEST_RADIOS) { |
| 1603 | return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet"); |
| 1604 | } |
| 1605 | |
| 1606 | final ConnectivityManager conn = (ConnectivityManager) context.getSystemService( |
| 1607 | Context.CONNECTIVITY_SERVICE); |
| 1608 | return conn.getNetworkInfo(TYPE_ETHERNET) != null; |
| 1609 | } |
| 1610 | |
| 1611 | /** |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 1612 | * Inflate a {@link Preference} style layout, adding the given {@link View} |
| 1613 | * widget into {@link android.R.id#widget_frame}. |
| 1614 | */ |
| 1615 | private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) { |
| 1616 | final View view = inflater.inflate(R.layout.preference, root, false); |
| 1617 | final LinearLayout widgetFrame = (LinearLayout) view.findViewById( |
| 1618 | android.R.id.widget_frame); |
| 1619 | widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT)); |
| 1620 | return view; |
| 1621 | } |
| 1622 | |
| 1623 | /** |
| 1624 | * Set {@link android.R.id#title} for a preference view inflated with |
Jeff Sharkey | 52c3f44 | 2011-06-23 00:39:38 -0700 | [diff] [blame] | 1625 | * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}. |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 1626 | */ |
| 1627 | private static void setPreferenceTitle(View parent, int resId) { |
| 1628 | final TextView title = (TextView) parent.findViewById(android.R.id.title); |
| 1629 | title.setText(resId); |
| 1630 | } |
| 1631 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1632 | /** |
| 1633 | * Set {@link android.R.id#summary} for a preference view inflated with |
| 1634 | * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}. |
| 1635 | */ |
| 1636 | private static void setPreferenceSummary(View parent, int resId) { |
| 1637 | final TextView summary = (TextView) parent.findViewById(android.R.id.summary); |
| 1638 | summary.setVisibility(View.VISIBLE); |
| 1639 | summary.setText(resId); |
| 1640 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1641 | } |