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