1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at 5 * 6 * http://www.apache.org/licenses/LICENSE-2.0 7 * 8 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 9 */ 10 11 package com.android.settings.dashboard.conditional; 12 13 import android.content.BroadcastReceiver; 14 import android.content.Context; 15 import android.content.Intent; 16 import android.content.IntentFilter; 17 import android.graphics.drawable.Drawable; 18 import android.net.ConnectivityManager; 19 import android.telephony.TelephonyManager; 20 21 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 22 import com.android.internal.telephony.TelephonyIntents; 23 import com.android.settings.R; 24 import com.android.settings.Settings; 25 26 public class CellularDataCondition extends Condition { 27 28 private final Receiver mReceiver; 29 30 private static final IntentFilter DATA_CONNECTION_FILTER = 31 new IntentFilter(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED); 32 CellularDataCondition(ConditionManager manager)33 public CellularDataCondition(ConditionManager manager) { 34 super(manager); 35 mReceiver = new Receiver(); 36 } 37 38 @Override refreshState()39 public void refreshState() { 40 ConnectivityManager connectivity = mManager.getContext().getSystemService( 41 ConnectivityManager.class); 42 TelephonyManager telephony = mManager.getContext().getSystemService(TelephonyManager.class); 43 if (!connectivity.isNetworkSupported(ConnectivityManager.TYPE_MOBILE) 44 || telephony.getSimState() != TelephonyManager.SIM_STATE_READY) { 45 setActive(false); 46 return; 47 } 48 setActive(!telephony.isDataEnabled()); 49 } 50 51 @Override getReceiver()52 protected BroadcastReceiver getReceiver() { 53 return mReceiver; 54 } 55 56 @Override getIntentFilter()57 protected IntentFilter getIntentFilter() { 58 return DATA_CONNECTION_FILTER; 59 } 60 61 @Override getIcon()62 public Drawable getIcon() { 63 return mManager.getContext().getDrawable(R.drawable.ic_cellular_off); 64 } 65 66 @Override getTitle()67 public CharSequence getTitle() { 68 return mManager.getContext().getString(R.string.condition_cellular_title); 69 } 70 71 @Override getSummary()72 public CharSequence getSummary() { 73 return mManager.getContext().getString(R.string.condition_cellular_summary); 74 } 75 76 @Override getActions()77 public CharSequence[] getActions() { 78 return new CharSequence[] { mManager.getContext().getString(R.string.condition_turn_on) }; 79 } 80 81 @Override onPrimaryClick()82 public void onPrimaryClick() { 83 mManager.getContext().startActivity(new Intent(mManager.getContext(), 84 Settings.DataUsageSummaryActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); 85 } 86 87 @Override onActionClick(int index)88 public void onActionClick(int index) { 89 if (index == 0) { 90 TelephonyManager telephony = mManager.getContext().getSystemService( 91 TelephonyManager.class); 92 telephony.setDataEnabled(true); 93 setActive(false); 94 } else { 95 throw new IllegalArgumentException("Unexpected index " + index); 96 } 97 } 98 99 @Override getMetricsConstant()100 public int getMetricsConstant() { 101 return MetricsEvent.SETTINGS_CONDITION_CELLULAR_DATA; 102 } 103 104 public static class Receiver extends BroadcastReceiver { 105 @Override onReceive(Context context, Intent intent)106 public void onReceive(Context context, Intent intent) { 107 if (TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED.equals( 108 intent.getAction())) { 109 CellularDataCondition condition = ConditionManager.get(context).getCondition( 110 CellularDataCondition.class); 111 if (condition != null) { 112 condition.refreshState(); 113 } 114 } 115 } 116 } 117 } 118