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