Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7ede236e authored by Danny Baumann's avatar Danny Baumann
Browse files

Factor out profile trigger code into separate class.

Makes it easier to extend the trigger mechanism later on.

Change-Id: Ied318e7efa7e1755022deecb8172f862943ca6ac
parent 1b0691b3
Loading
Loading
Loading
Loading
+3 −58
Original line number Diff line number Diff line
@@ -27,15 +27,11 @@ import android.app.Profile;
import android.app.ProfileGroup;
import android.app.ProfileManager;
import android.app.backup.BackupManager;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.XmlResourceParser;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiSsid;
import android.net.wifi.WifiInfo;
import android.os.Environment;
import android.os.RemoteException;
import android.os.UserHandle;
@@ -79,8 +75,7 @@ public class ProfileManagerService extends IProfileManager.Stub {
    private Context mContext;
    private boolean mDirty;
    private BackupManager mBackupManager;
    private WifiManager mWifiManager;
    private String mLastConnectedSSID;
    private ProfileTriggerHelper mTriggerHelper;

    private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
@@ -91,40 +86,6 @@ public class ProfileManagerService extends IProfileManager.Stub {
                initialize();
            } else if (action.equals(Intent.ACTION_SHUTDOWN)) {
                persistIfDirty();

            } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
                String activeSSID = getActiveSSID();
                int triggerState;
                if (activeSSID != null) {
                    triggerState = Profile.TriggerState.ON_CONNECT;
                    mLastConnectedSSID = activeSSID;
                } else {
                    triggerState = Profile.TriggerState.ON_DISCONNECT;
                }
                checkTriggers(Profile.TriggerType.WIFI, mLastConnectedSSID, triggerState);
            } else if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)
                    || action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
                int triggerState = action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)
                        ? Profile.TriggerState.ON_CONNECT : Profile.TriggerState.ON_DISCONNECT;
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                checkTriggers(Profile.TriggerType.BLUETOOTH, device.getAddress(), triggerState);
            }
        }

        private void checkTriggers(int type, String id, int newState) {
            for (Profile p : mProfiles.values()) {
                if (newState != p.getTrigger(type, id)) {
                    continue;
                }

                try {
                    if (!mActiveProfile.getUuid().equals(p.getUuid())) {
                        setActiveProfile(p, true);
                    }
                } catch (RemoteException e) {
                    Log.e(TAG, "Could not update profile on trigger", e);
                }
            }
        }
    };
@@ -132,8 +93,7 @@ public class ProfileManagerService extends IProfileManager.Stub {
    public ProfileManagerService(Context context) {
        mContext = context;
        mBackupManager = new BackupManager(mContext);
        mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        mLastConnectedSSID = getActiveSSID();
        mTriggerHelper = new ProfileTriggerHelper(mContext, this);

        mWildcardGroup = new NotificationGroup(
                context.getString(com.android.internal.R.string.wildcardProfile),
@@ -145,9 +105,6 @@ public class ProfileManagerService extends IProfileManager.Stub {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_LOCALE_CHANGED);
        filter.addAction(Intent.ACTION_SHUTDOWN);
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        mContext.registerReceiver(mIntentReceiver, filter);
    }

@@ -184,18 +141,6 @@ public class ProfileManagerService extends IProfileManager.Stub {
        }
    }

    private String getActiveSSID() {
        WifiInfo wifiinfo = mWifiManager.getConnectionInfo();
        if (wifiinfo == null) {
            return null;
        }
        WifiSsid ssid = wifiinfo.getWifiSsid();
        if (ssid == null) {
            return null;
        }
        return ssid.toString();
    }

    @Override
    public void resetAll() {
        enforceChangePermissions();
@@ -237,7 +182,7 @@ public class ProfileManagerService extends IProfileManager.Stub {
        }
    }

    private boolean setActiveProfile(Profile newActiveProfile, boolean doinit) throws RemoteException {
    /* package */ boolean setActiveProfile(Profile newActiveProfile, boolean doinit) throws RemoteException {
        /*
         * NOTE: Since this is not a public function, and all public functions
         * take either a string or a UUID, the active profile should always be
+109 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2013 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server;

import android.app.Profile;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiSsid;
import android.net.wifi.WifiInfo;
import android.os.RemoteException;
import android.util.Log;

import java.util.UUID;

public class ProfileTriggerHelper extends BroadcastReceiver {
    private static final String TAG = "ProfileTriggerService";

    private Context mContext;
    private ProfileManagerService mService;

    private WifiManager mWifiManager;
    private String mLastConnectedSSID;

    public ProfileTriggerHelper(Context context, ProfileManagerService service) {
        mContext = context;
        mService = service;

        mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        mLastConnectedSSID = getActiveSSID();

        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        mContext.registerReceiver(this, filter);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
            String activeSSID = getActiveSSID();
            int triggerState;

            if (activeSSID != null) {
                triggerState = Profile.TriggerState.ON_CONNECT;
                mLastConnectedSSID = activeSSID;
            } else {
                triggerState = Profile.TriggerState.ON_DISCONNECT;
            }
            checkTriggers(Profile.TriggerType.WIFI, mLastConnectedSSID, triggerState);
        } else if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)
                || action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
            int triggerState = action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)
                    ? Profile.TriggerState.ON_CONNECT : Profile.TriggerState.ON_DISCONNECT;
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            checkTriggers(Profile.TriggerType.BLUETOOTH, device.getAddress(), triggerState);
        }
    }

    private void checkTriggers(int type, String id, int newState) {
        try {
            for (Profile p : mService.getProfiles()) {
                if (newState != p.getTrigger(type, id)) {
                    continue;
                }

                UUID currentProfileUuid = mService.getActiveProfile().getUuid();
                if (!currentProfileUuid.equals(p.getUuid())) {
                    mService.setActiveProfile(p, true);
                }
            }
        } catch (RemoteException e) {
            Log.e(TAG, "Could not update profile on trigger", e);
        }
    }

    private String getActiveSSID() {
        WifiInfo wifiinfo = mWifiManager.getConnectionInfo();
        if (wifiinfo == null) {
            return null;
        }
        WifiSsid ssid = wifiinfo.getWifiSsid();
        if (ssid == null) {
            return null;
        }
        return ssid.toString();
    }
}