1 /* 2 * Copyright (C) 2015 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 package com.android.settings.dashboard.conditional; 17 18 import android.content.BroadcastReceiver; 19 import android.content.Context; 20 import android.content.Intent; 21 import android.content.IntentFilter; 22 import android.graphics.drawable.Drawable; 23 import android.net.ConnectivityManager; 24 import android.provider.Settings; 25 import android.util.Log; 26 27 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 28 import com.android.settings.R; 29 import com.android.settingslib.WirelessUtils; 30 31 public class AirplaneModeCondition extends Condition { 32 public static String TAG = "APM_Condition"; 33 34 private final Receiver mReceiver; 35 36 private static final IntentFilter AIRPLANE_MODE_FILTER = 37 new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED); 38 AirplaneModeCondition(ConditionManager conditionManager)39 public AirplaneModeCondition(ConditionManager conditionManager) { 40 super(conditionManager); 41 mReceiver = new Receiver(); 42 } 43 44 @Override refreshState()45 public void refreshState() { 46 Log.d(TAG, "APM condition refreshed"); 47 setActive(WirelessUtils.isAirplaneModeOn(mManager.getContext())); 48 } 49 50 @Override getReceiver()51 protected BroadcastReceiver getReceiver() { 52 return mReceiver; 53 } 54 55 @Override getIntentFilter()56 protected IntentFilter getIntentFilter() { 57 return AIRPLANE_MODE_FILTER; 58 } 59 60 @Override getIcon()61 public Drawable getIcon() { 62 return mManager.getContext().getDrawable(R.drawable.ic_airplane); 63 } 64 65 @Override setActive(boolean active)66 protected void setActive(boolean active) { 67 super.setActive(active); 68 Log.d(TAG, "setActive was called with " + active); 69 } 70 71 @Override getTitle()72 public CharSequence getTitle() { 73 return mManager.getContext().getString(R.string.condition_airplane_title); 74 } 75 76 @Override getSummary()77 public CharSequence getSummary() { 78 return mManager.getContext().getString(R.string.condition_airplane_summary); 79 } 80 81 @Override getActions()82 public CharSequence[] getActions() { 83 return new CharSequence[] {mManager.getContext().getString(R.string.condition_turn_off)}; 84 } 85 86 @Override onPrimaryClick()87 public void onPrimaryClick() { 88 mManager.getContext().startActivity( 89 new Intent(Settings.ACTION_WIRELESS_SETTINGS) 90 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); 91 } 92 93 @Override onActionClick(int index)94 public void onActionClick(int index) { 95 if (index == 0) { 96 ConnectivityManager.from(mManager.getContext()).setAirplaneMode(false); 97 setActive(false); 98 } else { 99 throw new IllegalArgumentException("Unexpected index " + index); 100 } 101 } 102 103 @Override getMetricsConstant()104 public int getMetricsConstant() { 105 return MetricsEvent.SETTINGS_CONDITION_AIRPLANE_MODE; 106 } 107 108 public static class Receiver extends BroadcastReceiver { 109 @Override onReceive(Context context, Intent intent)110 public void onReceive(Context context, Intent intent) { 111 if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) { 112 ConditionManager.get(context).getCondition(AirplaneModeCondition.class) 113 .refreshState(); 114 } 115 } 116 } 117 } 118