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