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 15package com.android.systemui.statusbar.policy; 16 17import android.content.Context; 18import android.net.INetworkPolicyListener; 19import android.net.NetworkPolicyManager; 20import android.os.Handler; 21import android.os.RemoteException; 22 23import java.util.ArrayList; 24 25public class DataSaverController { 26 27 private final Handler mHandler = new Handler(); 28 private final ArrayList<Listener> mListeners = new ArrayList<>(); 29 private final NetworkPolicyManager mPolicyManager; 30 31 public DataSaverController(Context context) { 32 mPolicyManager = NetworkPolicyManager.from(context); 33 } 34 35 private void handleRestrictBackgroundChanged(boolean isDataSaving) { 36 synchronized (mListeners) { 37 for (int i = 0; i < mListeners.size(); i++) { 38 mListeners.get(i).onDataSaverChanged(isDataSaving); 39 } 40 } 41 } 42 43 public void addListener(Listener listener) { 44 synchronized (mListeners) { 45 mListeners.add(listener); 46 if (mListeners.size() == 1) { 47 mPolicyManager.registerListener(mPolicyListener); 48 } 49 } 50 listener.onDataSaverChanged(isDataSaverEnabled()); 51 } 52 53 public void remListener(Listener listener) { 54 synchronized (mListeners) { 55 mListeners.remove(listener); 56 if (mListeners.size() == 0) { 57 mPolicyManager.unregisterListener(mPolicyListener); 58 } 59 } 60 } 61 62 public boolean isDataSaverEnabled() { 63 return mPolicyManager.getRestrictBackground(); 64 } 65 66 public void setDataSaverEnabled(boolean enabled) { 67 mPolicyManager.setRestrictBackground(enabled); 68 try { 69 mPolicyListener.onRestrictBackgroundChanged(enabled); 70 } catch (RemoteException e) { 71 } 72 } 73 74 private final INetworkPolicyListener mPolicyListener = new INetworkPolicyListener.Stub() { 75 @Override 76 public void onUidRulesChanged(int i, int i1) throws RemoteException { 77 } 78 79 @Override 80 public void onMeteredIfacesChanged(String[] strings) throws RemoteException { 81 } 82 83 @Override 84 public void onRestrictBackgroundChanged(final boolean isDataSaving) throws RemoteException { 85 mHandler.post(new Runnable() { 86 @Override 87 public void run() { 88 handleRestrictBackgroundChanged(isDataSaving); 89 } 90 }); 91 } 92 93 @Override 94 public void onRestrictBackgroundWhitelistChanged(int uid, boolean whitelisted) { 95 } 96 @Override 97 public void onRestrictBackgroundBlacklistChanged(int uid, boolean blacklisted) { 98 } 99 }; 100 101 public interface Listener { 102 void onDataSaverChanged(boolean isDataSaving); 103 } 104 105} 106