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.Icon; 18 import android.net.ConnectivityManager; 19 import android.telephony.TelephonyManager; 20 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 21 import com.android.internal.telephony.TelephonyIntents; 22 import com.android.settings.R; 23 import com.android.settings.Settings; 24 25 public class CellularDataCondition extends Condition { 26 27 private final Receiver mReceiver; 28 29 private static final IntentFilter DATA_CONNECTION_FILTER = 30 new IntentFilter(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED); 31 CellularDataCondition(ConditionManager manager)32 public CellularDataCondition(ConditionManager manager) { 33 super(manager); 34 mReceiver = new Receiver(); 35 } 36 37 @Override refreshState()38 public void refreshState() { 39 ConnectivityManager connectivity = mManager.getContext().getSystemService( 40 ConnectivityManager.class); 41 TelephonyManager telephony = mManager.getContext().getSystemService(TelephonyManager.class); 42 if (!connectivity.isNetworkSupported(ConnectivityManager.TYPE_MOBILE) 43 || telephony.getSimState() != TelephonyManager.SIM_STATE_READY) { 44 setActive(false); 45 return; 46 } 47 setActive(!telephony.getDataEnabled()); 48 } 49 50 @Override getReceiver()51 protected BroadcastReceiver getReceiver() { 52 return mReceiver; 53 } 54 55 @Override getIntentFilter()56 protected IntentFilter getIntentFilter() { 57 return DATA_CONNECTION_FILTER; 58 } 59 60 @Override getIcon()61 public Icon getIcon() { 62 return Icon.createWithResource(mManager.getContext(), R.drawable.ic_cellular_off); 63 } 64 65 @Override getTitle()66 public CharSequence getTitle() { 67 return mManager.getContext().getString(R.string.condition_cellular_title); 68 } 69 70 @Override getSummary()71 public CharSequence getSummary() { 72 return mManager.getContext().getString(R.string.condition_cellular_summary); 73 } 74 75 @Override getActions()76 public CharSequence[] getActions() { 77 return new CharSequence[] { mManager.getContext().getString(R.string.condition_turn_on) }; 78 } 79 80 @Override onPrimaryClick()81 public void onPrimaryClick() { 82 mManager.getContext().startActivity(new Intent(mManager.getContext(), 83 Settings.DataUsageSummaryActivity.class)); 84 } 85 86 @Override onActionClick(int index)87 public void onActionClick(int index) { 88 if (index == 0) { 89 TelephonyManager telephony = mManager.getContext().getSystemService( 90 TelephonyManager.class); 91 telephony.setDataEnabled(true); 92 setActive(false); 93 } else { 94 throw new IllegalArgumentException("Unexpected index " + index); 95 } 96 } 97 98 @Override getMetricsConstant()99 public int getMetricsConstant() { 100 return MetricsEvent.SETTINGS_CONDITION_CELLULAR_DATA; 101 } 102 103 public static class Receiver extends BroadcastReceiver { 104 @Override onReceive(Context context, Intent intent)105 public void onReceive(Context context, Intent intent) { 106 if (TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED.equals( 107 intent.getAction())) { 108 ConditionManager.get(context).getCondition(CellularDataCondition.class) 109 .refreshState(); 110 } 111 } 112 } 113 } 114