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.car.settings.quicksettings;
18 
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.graphics.drawable.Drawable;
23 import android.telephony.TelephonyManager;
24 import android.view.View;
25 
26 import com.android.car.settings.R;
27 import com.android.car.settings.common.CarSettingActivities;
28 import com.android.car.settings.common.FragmentHost;
29 import com.android.settingslib.net.DataUsageController;
30 
31 /**
32  * Controls cellular on quick setting page.
33  */
34 public class CelluarTile implements QuickSettingGridAdapter.Tile, DataUsageController.Callback {
35     private final Context mContext;
36     private final StateChangedListener mStateChangedListener;
37     private final DataUsageController mDataUsageController;
38     @Nullable
39     private final String mCarrierName;
40     private final boolean mAvailable;
41     private final View.OnLongClickListener mLaunchDisplaySettings;
42 
43     private State mState = State.ON;
44 
CelluarTile(Context context, StateChangedListener stateChangedListener, FragmentHost fragmentHost)45     CelluarTile(Context context, StateChangedListener stateChangedListener,
46             FragmentHost fragmentHost) {
47         mStateChangedListener = stateChangedListener;
48         mContext = context;
49         TelephonyManager manager =
50                 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
51         mDataUsageController = new DataUsageController(mContext);
52         mDataUsageController.setCallback(this);
53         mAvailable = mDataUsageController.isMobileDataSupported();
54         mState = mAvailable && mDataUsageController.isMobileDataEnabled() ? State.ON : State.OFF;
55         mCarrierName = mAvailable ? manager.getNetworkOperatorName() : null;
56 
57         mLaunchDisplaySettings = v -> {
58             context.startActivity(new Intent(context,
59                     CarSettingActivities.MobileNetworkActivity.class));
60             return true;
61         };
62     }
63 
64     @Override
onMobileDataEnabled(boolean enabled)65     public void onMobileDataEnabled(boolean enabled) {
66         mState = enabled ? State.ON : State.OFF;
67     }
68 
69     @Override
getOnLongClickListener()70     public View.OnLongClickListener getOnLongClickListener() {
71         return mLaunchDisplaySettings;
72     }
73 
74     @Override
isAvailable()75     public boolean isAvailable() {
76         return mAvailable;
77     }
78 
79     @Override
getIcon()80     public Drawable getIcon() {
81         return mContext.getDrawable(R.drawable.ic_cellular_data);
82     }
83 
84     @Override
85     @Nullable
getText()86     public String getText() {
87         return mCarrierName;
88     }
89 
90     @Override
getState()91     public State getState() {
92         return mState;
93     }
94 
95     @Override
start()96     public void start() {
97     }
98 
99     @Override
stop()100     public void stop() {
101     }
102 
103     @Override
onClick(View v)104     public void onClick(View v) {
105         mDataUsageController.setMobileDataEnabled(!mDataUsageController.isMobileDataEnabled());
106     }
107 }
108