Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -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 | |
| 19 | import static android.net.NetworkPolicyManager.POLICY_NONE; |
Jeff Sharkey | 1a3e41d | 2011-06-16 15:08:08 -0700 | [diff] [blame] | 20 | import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND; |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 21 | import static com.android.settings.DataUsageSummary.getHistoryBounds; |
| 22 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 23 | import android.app.AlertDialog; |
| 24 | import android.app.Dialog; |
| 25 | import android.app.DialogFragment; |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 26 | import android.app.Fragment; |
| 27 | import android.content.Context; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 28 | import android.content.DialogInterface; |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 29 | import android.content.Intent; |
| 30 | import android.content.pm.PackageManager; |
| 31 | import android.graphics.Color; |
| 32 | import android.net.INetworkPolicyManager; |
| 33 | import android.net.INetworkStatsService; |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 34 | import android.net.NetworkPolicyManager; |
Jeff Sharkey | 827fde3 | 2011-06-19 20:56:31 -0700 | [diff] [blame] | 35 | import android.net.NetworkStats; |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 36 | import android.net.NetworkStatsHistory; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 37 | import android.net.NetworkTemplate; |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 38 | import android.os.Bundle; |
| 39 | import android.os.RemoteException; |
| 40 | import android.os.ServiceManager; |
| 41 | import android.preference.CheckBoxPreference; |
| 42 | import android.preference.Preference; |
| 43 | import android.text.format.DateUtils; |
| 44 | import android.text.format.Formatter; |
| 45 | import android.util.Log; |
| 46 | import android.view.LayoutInflater; |
| 47 | import android.view.View; |
| 48 | import android.view.View.OnClickListener; |
| 49 | import android.view.ViewGroup; |
| 50 | import android.widget.Button; |
| 51 | import android.widget.FrameLayout; |
| 52 | import android.widget.LinearLayout; |
| 53 | import android.widget.TextView; |
| 54 | |
| 55 | import com.android.settings.widget.DataUsageChartView; |
| 56 | import com.android.settings.widget.DataUsageChartView.DataUsageChartListener; |
| 57 | |
| 58 | public class DataUsageAppDetail extends Fragment { |
| 59 | private static final String TAG = "DataUsage"; |
| 60 | private static final boolean LOGD = true; |
| 61 | |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 62 | public static final String EXTRA_UID = "uid"; |
| 63 | public static final String EXTRA_NETWORK_TEMPLATE = "networkTemplate"; |
| 64 | |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 65 | private int mUid; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 66 | private NetworkTemplate mTemplate; |
| 67 | |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 68 | private Intent mAppSettingsIntent; |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 69 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 70 | private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict"; |
| 71 | |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 72 | private INetworkStatsService mStatsService; |
| 73 | private INetworkPolicyManager mPolicyService; |
| 74 | |
| 75 | private CheckBoxPreference mRestrictBackground; |
| 76 | private View mRestrictBackgroundView; |
| 77 | |
| 78 | private FrameLayout mChartContainer; |
| 79 | private TextView mTitle; |
| 80 | private TextView mText1; |
| 81 | private Button mAppSettings; |
| 82 | private LinearLayout mSwitches; |
| 83 | |
| 84 | private DataUsageChartView mChart; |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 85 | private NetworkStatsHistory mHistory; |
| 86 | |
| 87 | @Override |
| 88 | public void onCreate(Bundle savedInstanceState) { |
| 89 | super.onCreate(savedInstanceState); |
| 90 | |
| 91 | mStatsService = INetworkStatsService.Stub.asInterface( |
| 92 | ServiceManager.getService(Context.NETWORK_STATS_SERVICE)); |
| 93 | mPolicyService = INetworkPolicyManager.Stub.asInterface( |
| 94 | ServiceManager.getService(Context.NETWORK_POLICY_SERVICE)); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 99 | Bundle savedInstanceState) { |
| 100 | |
| 101 | final Context context = inflater.getContext(); |
| 102 | final View view = inflater.inflate(R.layout.data_usage_detail, container, false); |
| 103 | |
| 104 | mChartContainer = (FrameLayout) view.findViewById(R.id.chart_container); |
| 105 | mTitle = (TextView) view.findViewById(android.R.id.title); |
| 106 | mText1 = (TextView) view.findViewById(android.R.id.text1); |
| 107 | mAppSettings = (Button) view.findViewById(R.id.data_usage_app_settings); |
| 108 | mSwitches = (LinearLayout) view.findViewById(R.id.switches); |
| 109 | |
| 110 | mRestrictBackground = new CheckBoxPreference(context); |
| 111 | mRestrictBackground.setTitle(R.string.data_usage_app_restrict_background); |
| 112 | mRestrictBackground.setSummary(R.string.data_usage_app_restrict_background_summary); |
| 113 | |
| 114 | // kick refresh once to force-create views |
| 115 | refreshPreferenceViews(); |
| 116 | |
| 117 | mSwitches.addView(mRestrictBackgroundView); |
| 118 | mRestrictBackgroundView.setOnClickListener(mRestrictBackgroundListener); |
| 119 | |
| 120 | mAppSettings.setOnClickListener(mAppSettingsListener); |
| 121 | |
| 122 | mChart = new DataUsageChartView(context); |
| 123 | mChartContainer.addView(mChart); |
| 124 | |
| 125 | mChart.setListener(mChartListener); |
| 126 | mChart.setChartColor(Color.parseColor("#d88d3a"), Color.parseColor("#c0ba7f3e"), |
| 127 | Color.parseColor("#88566abc")); |
| 128 | |
| 129 | return view; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void onResume() { |
| 134 | super.onResume(); |
| 135 | |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 136 | updateBody(); |
| 137 | } |
| 138 | |
| 139 | private void updateBody() { |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 140 | final PackageManager pm = getActivity().getPackageManager(); |
| 141 | |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 142 | mUid = getArguments().getInt(EXTRA_UID); |
| 143 | mTemplate = getArguments().getParcelable(EXTRA_NETWORK_TEMPLATE); |
| 144 | |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 145 | mTitle.setText(pm.getNameForUid(mUid)); |
| 146 | |
| 147 | // enable settings button when package provides it |
| 148 | // TODO: target torwards entire UID instead of just first package |
| 149 | final String[] packageNames = pm.getPackagesForUid(mUid); |
| 150 | if (packageNames != null && packageNames.length > 0) { |
| 151 | mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE); |
| 152 | mAppSettingsIntent.setPackage(packageNames[0]); |
| 153 | mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT); |
| 154 | |
| 155 | final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null; |
| 156 | mAppSettings.setEnabled(matchFound); |
| 157 | |
| 158 | } else { |
| 159 | mAppSettingsIntent = null; |
| 160 | mAppSettings.setEnabled(false); |
| 161 | } |
| 162 | |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 163 | try { |
| 164 | // load stats for current uid and template |
| 165 | // TODO: read template from extras |
Jeff Sharkey | 827fde3 | 2011-06-19 20:56:31 -0700 | [diff] [blame] | 166 | mHistory = mStatsService.getHistoryForUid(mTemplate, mUid, NetworkStats.TAG_NONE); |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 167 | } catch (RemoteException e) { |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 168 | // since we can't do much without history, and we don't want to |
| 169 | // leave with half-baked UI, we bail hard. |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 170 | throw new RuntimeException("problem reading network stats", e); |
| 171 | } |
| 172 | |
| 173 | // bind chart to historical stats |
| 174 | mChart.bindNetworkStats(mHistory); |
| 175 | |
| 176 | // show entire history known |
| 177 | final long[] bounds = getHistoryBounds(mHistory); |
| 178 | mChart.setVisibleRange(bounds[0], bounds[1] + DateUtils.WEEK_IN_MILLIS, bounds[1]); |
| 179 | updateDetailData(); |
| 180 | |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 181 | final Context context = getActivity(); |
| 182 | if (NetworkPolicyManager.isUidValidForPolicy(context, mUid)) { |
| 183 | mRestrictBackgroundView.setVisibility(View.VISIBLE); |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 184 | |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 185 | final int uidPolicy; |
| 186 | try { |
| 187 | uidPolicy = mPolicyService.getUidPolicy(mUid); |
| 188 | } catch (RemoteException e) { |
| 189 | // since we can't do much without policy, we bail hard. |
| 190 | throw new RuntimeException("problem reading network policy", e); |
| 191 | } |
| 192 | |
| 193 | // update policy checkbox |
Jeff Sharkey | 1a3e41d | 2011-06-16 15:08:08 -0700 | [diff] [blame] | 194 | final boolean restrictBackground = (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0; |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 195 | mRestrictBackground.setChecked(restrictBackground); |
| 196 | |
| 197 | // kick preference views so they rebind from changes above |
| 198 | refreshPreferenceViews(); |
| 199 | |
| 200 | } else { |
| 201 | mRestrictBackgroundView.setVisibility(View.GONE); |
| 202 | } |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | private void updateDetailData() { |
| 206 | if (LOGD) Log.d(TAG, "updateDetailData()"); |
| 207 | |
| 208 | final Context context = mChart.getContext(); |
| 209 | final long[] range = mChart.getInspectRange(); |
| 210 | final long[] total = mHistory.getTotalData(range[0], range[1], null); |
| 211 | final long totalCombined = total[0] + total[1]; |
| 212 | mText1.setText(Formatter.formatFileSize(context, totalCombined)); |
| 213 | } |
| 214 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 215 | private void setRestrictBackground(boolean restrictBackground) { |
| 216 | if (LOGD) Log.d(TAG, "setRestrictBackground()"); |
| 217 | try { |
| 218 | mPolicyService.setUidPolicy( |
Jeff Sharkey | 1a3e41d | 2011-06-16 15:08:08 -0700 | [diff] [blame] | 219 | mUid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 220 | } catch (RemoteException e) { |
| 221 | throw new RuntimeException("unable to save policy", e); |
| 222 | } |
| 223 | |
| 224 | mRestrictBackground.setChecked(restrictBackground); |
| 225 | refreshPreferenceViews(); |
| 226 | } |
| 227 | |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 228 | /** |
| 229 | * Force rebind of hijacked {@link Preference} views. |
| 230 | */ |
| 231 | private void refreshPreferenceViews() { |
| 232 | mRestrictBackgroundView = mRestrictBackground.getView(mRestrictBackgroundView, mSwitches); |
| 233 | } |
| 234 | |
| 235 | private DataUsageChartListener mChartListener = new DataUsageChartListener() { |
| 236 | /** {@inheritDoc} */ |
| 237 | public void onInspectRangeChanged() { |
| 238 | if (LOGD) Log.d(TAG, "onInspectRangeChanged()"); |
| 239 | updateDetailData(); |
| 240 | } |
| 241 | |
| 242 | /** {@inheritDoc} */ |
| 243 | public void onWarningChanged() { |
| 244 | // ignored |
| 245 | } |
| 246 | |
| 247 | /** {@inheritDoc} */ |
| 248 | public void onLimitChanged() { |
| 249 | // ignored |
| 250 | } |
| 251 | }; |
| 252 | |
| 253 | private OnClickListener mAppSettingsListener = new OnClickListener() { |
| 254 | /** {@inheritDoc} */ |
| 255 | public void onClick(View v) { |
| 256 | // TODO: target torwards entire UID instead of just first package |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 257 | startActivity(mAppSettingsIntent); |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 258 | } |
| 259 | }; |
| 260 | |
| 261 | private OnClickListener mRestrictBackgroundListener = new OnClickListener() { |
| 262 | /** {@inheritDoc} */ |
| 263 | public void onClick(View v) { |
| 264 | final boolean restrictBackground = !mRestrictBackground.isChecked(); |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 265 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 266 | if (restrictBackground) { |
| 267 | // enabling restriction; show confirmation dialog which |
| 268 | // eventually calls setRestrictBackground() once user confirms. |
| 269 | ConfirmRestrictFragment.show(DataUsageAppDetail.this); |
| 270 | } else { |
| 271 | setRestrictBackground(false); |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | }; |
| 275 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 276 | /** |
| 277 | * Dialog to request user confirmation before setting |
Jeff Sharkey | 1a3e41d | 2011-06-16 15:08:08 -0700 | [diff] [blame] | 278 | * {@link #POLICY_REJECT_METERED_BACKGROUND}. |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 279 | */ |
| 280 | public static class ConfirmRestrictFragment extends DialogFragment { |
| 281 | public static void show(DataUsageAppDetail parent) { |
| 282 | final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment(); |
| 283 | dialog.setTargetFragment(parent, 0); |
| 284 | dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT); |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 289 | final Context context = getActivity(); |
| 290 | |
| 291 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 292 | builder.setTitle(R.string.data_usage_app_restrict_dialog_title); |
| 293 | builder.setMessage(R.string.data_usage_app_restrict_dialog); |
| 294 | |
| 295 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
| 296 | public void onClick(DialogInterface dialog, int which) { |
| 297 | final DataUsageAppDetail target = (DataUsageAppDetail) getTargetFragment(); |
| 298 | if (target != null) { |
| 299 | target.setRestrictBackground(true); |
| 300 | } |
| 301 | } |
| 302 | }); |
| 303 | builder.setNegativeButton(android.R.string.cancel, null); |
| 304 | |
| 305 | return builder.create(); |
| 306 | } |
| 307 | } |
| 308 | |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 309 | } |