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