Home | History | Annotate | Download | only in deviceinfo
      1 /*
      2  * Copyright (C) 2017 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.settingslib.deviceinfo;
     18 
     19 import android.content.BroadcastReceiver;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.IntentFilter;
     23 import android.os.Handler;
     24 import android.os.Message;
     25 
     26 import com.android.internal.util.ArrayUtils;
     27 import com.android.settingslib.core.AbstractPreferenceController;
     28 import com.android.settingslib.core.lifecycle.Lifecycle;
     29 import com.android.settingslib.core.lifecycle.LifecycleObserver;
     30 import com.android.settingslib.core.lifecycle.events.OnStart;
     31 import com.android.settingslib.core.lifecycle.events.OnStop;
     32 
     33 import java.lang.ref.WeakReference;
     34 
     35 /**
     36  * Base class for preference controllers which listen to connectivity broadcasts
     37  */
     38 public abstract class AbstractConnectivityPreferenceController
     39         extends AbstractPreferenceController implements LifecycleObserver, OnStart, OnStop {
     40 
     41     private final BroadcastReceiver mConnectivityReceiver = new BroadcastReceiver() {
     42         @Override
     43         public void onReceive(Context context, Intent intent) {
     44             String action = intent.getAction();
     45             if (ArrayUtils.contains(getConnectivityIntents(), action)) {
     46                 getHandler().sendEmptyMessage(EVENT_UPDATE_CONNECTIVITY);
     47             }
     48         }
     49     };
     50 
     51     private static final int EVENT_UPDATE_CONNECTIVITY = 600;
     52 
     53     private Handler mHandler;
     54 
     55     public AbstractConnectivityPreferenceController(Context context, Lifecycle lifecycle) {
     56         super(context);
     57         if (lifecycle != null) {
     58             lifecycle.addObserver(this);
     59         }
     60     }
     61 
     62     @Override
     63     public void onStop() {
     64         mContext.unregisterReceiver(mConnectivityReceiver);
     65     }
     66 
     67     @Override
     68     public void onStart() {
     69         final IntentFilter connectivityIntentFilter = new IntentFilter();
     70         final String[] intents = getConnectivityIntents();
     71         for (String intent : intents) {
     72             connectivityIntentFilter.addAction(intent);
     73         }
     74 
     75         mContext.registerReceiver(mConnectivityReceiver, connectivityIntentFilter,
     76                 android.Manifest.permission.CHANGE_NETWORK_STATE, null);
     77     }
     78 
     79     protected abstract String[] getConnectivityIntents();
     80 
     81     protected abstract void updateConnectivity();
     82 
     83     private Handler getHandler() {
     84         if (mHandler == null) {
     85             mHandler = new ConnectivityEventHandler(this);
     86         }
     87         return mHandler;
     88     }
     89 
     90     private static class ConnectivityEventHandler extends Handler {
     91         private WeakReference<AbstractConnectivityPreferenceController> mPreferenceController;
     92 
     93         public ConnectivityEventHandler(AbstractConnectivityPreferenceController activity) {
     94             mPreferenceController = new WeakReference<>(activity);
     95         }
     96 
     97         @Override
     98         public void handleMessage(Message msg) {
     99             AbstractConnectivityPreferenceController preferenceController
    100                     = mPreferenceController.get();
    101             if (preferenceController == null) {
    102                 return;
    103             }
    104 
    105             switch (msg.what) {
    106                 case EVENT_UPDATE_CONNECTIVITY:
    107                     preferenceController.updateConnectivity();
    108                     break;
    109                 default:
    110                     throw new IllegalStateException("Unknown message " + msg.what);
    111             }
    112         }
    113     }
    114 }
    115