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