1 /*
2  * Copyright (C) 2018 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 
17 package com.android.settings.homepage.contextualcards.conditional;
18 
19 import android.app.settings.SettingsEnums;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.net.ConnectivityManager;
25 import android.provider.Settings;
26 
27 import com.android.settings.R;
28 import com.android.settings.homepage.contextualcards.ContextualCard;
29 import com.android.settingslib.WirelessUtils;
30 
31 import java.util.Objects;
32 
33 public class AirplaneModeConditionController implements ConditionalCardController {
34 
35     static final int ID = Objects.hash("AirplaneModeConditionController");
36 
37     private static final IntentFilter AIRPLANE_MODE_FILTER =
38             new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
39 
40     private final ConditionManager mConditionManager;
41     private final Context mAppContext;
42     private final Receiver mReceiver;
43 
AirplaneModeConditionController(Context appContext, ConditionManager conditionManager)44     public AirplaneModeConditionController(Context appContext, ConditionManager conditionManager) {
45         mAppContext = appContext;
46         mConditionManager = conditionManager;
47         mReceiver = new Receiver();
48     }
49 
50     @Override
getId()51     public long getId() {
52         return ID;
53     }
54 
55     @Override
isDisplayable()56     public boolean isDisplayable() {
57         return WirelessUtils.isAirplaneModeOn(mAppContext);
58     }
59 
60     @Override
onPrimaryClick(Context context)61     public void onPrimaryClick(Context context) {
62         context.startActivity(
63                 new Intent(Settings.ACTION_WIRELESS_SETTINGS));
64     }
65 
66     @Override
onActionClick()67     public void onActionClick() {
68         ConnectivityManager.from(mAppContext).setAirplaneMode(false);
69     }
70 
71     @Override
buildContextualCard()72     public ContextualCard buildContextualCard() {
73         return new ConditionalContextualCard.Builder()
74                 .setConditionId(ID)
75                 .setMetricsConstant(SettingsEnums.SETTINGS_CONDITION_AIRPLANE_MODE)
76                 .setActionText(mAppContext.getText(R.string.condition_turn_off))
77                 .setName(mAppContext.getPackageName() + "/"
78                         + mAppContext.getText(R.string.condition_airplane_title))
79                 .setTitleText(mAppContext.getText(R.string.condition_airplane_title).toString())
80                 .setSummaryText(mAppContext.getText(R.string.condition_airplane_summary).toString())
81                 .setIconDrawable(mAppContext.getDrawable(R.drawable.ic_airplanemode_active))
82                 .setViewType(ConditionContextualCardRenderer.VIEW_TYPE_HALF_WIDTH)
83                 .build();
84     }
85 
86     @Override
startMonitoringStateChange()87     public void startMonitoringStateChange() {
88         mAppContext.registerReceiver(mReceiver, AIRPLANE_MODE_FILTER);
89     }
90 
91     @Override
stopMonitoringStateChange()92     public void stopMonitoringStateChange() {
93         mAppContext.unregisterReceiver(mReceiver);
94     }
95 
96     public class Receiver extends BroadcastReceiver {
97         @Override
onReceive(Context context, Intent intent)98         public void onReceive(Context context, Intent intent) {
99             if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) {
100                 mConditionManager.onConditionChanged();
101             }
102         }
103     }
104 }
105