Home | History | Annotate | Download | only in policy
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
      5  * except in compliance with the License. You may obtain a copy of the License at
      6  *
      7  *      http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software distributed under the
     10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     11  * KIND, either express or implied. See the License for the specific language governing
     12  * permissions and limitations under the License.
     13  */
     14 
     15 package com.android.systemui.statusbar.policy;
     16 
     17 import android.content.Context;
     18 import android.net.INetworkPolicyListener;
     19 import android.net.NetworkPolicyManager;
     20 import android.os.Handler;
     21 import android.os.Looper;
     22 import android.os.RemoteException;
     23 
     24 import com.android.systemui.statusbar.policy.DataSaverController.Listener;
     25 
     26 import java.util.ArrayList;
     27 
     28 public class DataSaverControllerImpl implements DataSaverController {
     29 
     30     private final Handler mHandler = new Handler(Looper.getMainLooper());
     31     private final ArrayList<Listener> mListeners = new ArrayList<>();
     32     private final NetworkPolicyManager mPolicyManager;
     33 
     34     public DataSaverControllerImpl(Context context) {
     35         mPolicyManager = NetworkPolicyManager.from(context);
     36     }
     37 
     38     private void handleRestrictBackgroundChanged(boolean isDataSaving) {
     39         synchronized (mListeners) {
     40             for (int i = 0; i < mListeners.size(); i++) {
     41                 mListeners.get(i).onDataSaverChanged(isDataSaving);
     42             }
     43         }
     44     }
     45 
     46     public void addCallback(Listener listener) {
     47         synchronized (mListeners) {
     48             mListeners.add(listener);
     49             if (mListeners.size() == 1) {
     50                 mPolicyManager.registerListener(mPolicyListener);
     51             }
     52         }
     53         listener.onDataSaverChanged(isDataSaverEnabled());
     54     }
     55 
     56     public void removeCallback(Listener listener) {
     57         synchronized (mListeners) {
     58             mListeners.remove(listener);
     59             if (mListeners.size() == 0) {
     60                 mPolicyManager.unregisterListener(mPolicyListener);
     61             }
     62         }
     63     }
     64 
     65     public boolean isDataSaverEnabled() {
     66         return mPolicyManager.getRestrictBackground();
     67     }
     68 
     69     public void setDataSaverEnabled(boolean enabled) {
     70         mPolicyManager.setRestrictBackground(enabled);
     71         try {
     72             mPolicyListener.onRestrictBackgroundChanged(enabled);
     73         } catch (RemoteException e) {
     74         }
     75     }
     76 
     77     private final INetworkPolicyListener mPolicyListener = new NetworkPolicyManager.Listener() {
     78         @Override
     79         public void onRestrictBackgroundChanged(final boolean isDataSaving) {
     80             mHandler.post(new Runnable() {
     81                 @Override
     82                 public void run() {
     83                     handleRestrictBackgroundChanged(isDataSaving);
     84                 }
     85             });
     86         }
     87     };
     88 
     89 }
     90