• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.settings.datausage;
16 
17 import android.app.AlertDialog;
18 import android.content.Context;
19 import android.content.DialogInterface;
20 import android.net.NetworkPolicyManager;
21 import android.support.v7.preference.PreferenceViewHolder;
22 import android.util.AttributeSet;
23 import android.view.View;
24 import android.widget.Checkable;
25 import com.android.settings.CustomDialogPreference;
26 import com.android.settings.R;
27 import com.android.settings.Utils;
28 import com.android.settings.dashboard.conditional.BackgroundDataCondition;
29 import com.android.settings.dashboard.conditional.ConditionManager;
30 
31 public class RestrictBackgroundDataPreference extends CustomDialogPreference {
32 
33     private NetworkPolicyManager mPolicyManager;
34     private boolean mChecked;
35 
RestrictBackgroundDataPreference(Context context, AttributeSet attrs)36     public RestrictBackgroundDataPreference(Context context, AttributeSet attrs) {
37         super(context, attrs, android.R.attr.switchPreferenceStyle);
38     }
39 
40     @Override
onAttached()41     public void onAttached() {
42         super.onAttached();
43         mPolicyManager = NetworkPolicyManager.from(getContext());
44         setChecked(mPolicyManager.getRestrictBackground());
45     }
46 
setRestrictBackground(boolean restrictBackground)47     public void setRestrictBackground(boolean restrictBackground) {
48         mPolicyManager.setRestrictBackground(restrictBackground);
49         setChecked(restrictBackground);
50         ConditionManager.get(getContext()).getCondition(BackgroundDataCondition.class)
51                 .refreshState();
52     }
53 
setChecked(boolean checked)54     private void setChecked(boolean checked) {
55         if (mChecked == checked) return;
56         mChecked = checked;
57         notifyChanged();
58     }
59 
60     @Override
onBindViewHolder(PreferenceViewHolder holder)61     public void onBindViewHolder(PreferenceViewHolder holder) {
62         super.onBindViewHolder(holder);
63         View switchView = holder.findViewById(android.R.id.switch_widget);
64         switchView.setClickable(false);
65         ((Checkable) switchView).setChecked(mChecked);
66     }
67 
68     @Override
performClick(View view)69     protected void performClick(View view) {
70         if (mChecked) {
71             setRestrictBackground(false);
72         } else {
73             super.performClick(view);
74         }
75     }
76 
77     @Override
onPrepareDialogBuilder(AlertDialog.Builder builder, DialogInterface.OnClickListener listener)78     protected void onPrepareDialogBuilder(AlertDialog.Builder builder,
79             DialogInterface.OnClickListener listener) {
80         super.onPrepareDialogBuilder(builder, listener);
81         builder.setTitle(R.string.data_usage_restrict_background_title);
82         if (Utils.hasMultipleUsers(getContext())) {
83             builder.setMessage(R.string.data_usage_restrict_background_multiuser);
84         } else {
85             builder.setMessage(R.string.data_usage_restrict_background);
86         }
87 
88         builder.setPositiveButton(android.R.string.ok, listener);
89         builder.setNegativeButton(android.R.string.cancel, null);
90     }
91 
92     @Override
onClick(DialogInterface dialog, int which)93     protected void onClick(DialogInterface dialog, int which) {
94         if (which != DialogInterface.BUTTON_POSITIVE) {
95             return;
96         }
97         setRestrictBackground(true);
98     }
99 }
100