1 /*
2  * Copyright (C) 2020 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.tv.settings.accessories;
18 
19 import static com.android.tv.settings.accessories.ConnectedDevicesSliceProvider.KEY_BLUETOOTH_TOGGLE;
20 import static com.android.tv.settings.accessories.ConnectedDevicesSliceProvider.KEY_CONNECT;
21 import static com.android.tv.settings.accessories.ConnectedDevicesSliceProvider.KEY_DISCONNECT;
22 import static com.android.tv.settings.accessories.ConnectedDevicesSliceProvider.KEY_EXTRAS_DEVICE;
23 import static com.android.tv.settings.accessories.ConnectedDevicesSliceProvider.KEY_FORGET;
24 import static com.android.tv.settings.accessories.ConnectedDevicesSliceProvider.KEY_RENAME;
25 import static com.android.tv.settings.accessories.ConnectedDevicesSliceProvider.YES;
26 import static com.android.tv.settings.accessories.ConnectedDevicesSliceUtils.DIRECTION_BACK;
27 import static com.android.tv.settings.accessories.ConnectedDevicesSliceUtils.EXTRAS_DIRECTION;
28 
29 import android.app.Activity;
30 import android.bluetooth.BluetoothDevice;
31 import android.content.ComponentName;
32 import android.content.Context;
33 import android.content.Intent;
34 import android.content.ServiceConnection;
35 import android.os.Bundle;
36 import android.os.IBinder;
37 
38 /**
39  * The {@Activity} for handling confirmation UI of Bluetooth-related actions such as connection and
40  * renaming.
41  */
42 public class BluetoothActionActivity extends Activity implements BluetoothActionFragment.Listener {
43 
44     private boolean mBtDeviceServiceBound;
45     private BluetoothDevice mDevice;
46     private BluetoothDevicesService.LocalBinder mBtDeviceServiceBinder;
47 
48     private final ServiceConnection mBtDeviceServiceConnection = new SimplifiedConnection() {
49 
50         @Override
51         public void onServiceConnected(ComponentName className, IBinder service) {
52             mBtDeviceServiceBinder = (BluetoothDevicesService.LocalBinder) service;
53             mBtDeviceServiceBound = true;
54         }
55 
56         @Override
57         protected void cleanUp() {
58             mBtDeviceServiceBound = false;
59             mBtDeviceServiceBinder = null;
60         }
61     };
62 
63     @Override
onCreate(Bundle savedInstanceState)64     public void onCreate(Bundle savedInstanceState) {
65         super.onCreate(savedInstanceState);
66         mDevice = getIntent().getParcelableExtra(KEY_EXTRAS_DEVICE);
67         bindService(new Intent(this, AccessoryUtils.getBluetoothDeviceServiceClass()),
68                 mBtDeviceServiceConnection, Context.BIND_AUTO_CREATE);
69         BluetoothActionFragment responseFragment = new BluetoothActionFragment();
70         responseFragment.setArguments(getIntent().getExtras());
71         getFragmentManager().beginTransaction().add(android.R.id.content, responseFragment)
72                 .commit();
73     }
74 
75     @Override
onChoice(String key, int choice)76     public void onChoice(String key, int choice) {
77         BluetoothDeviceProvider provider = mBtDeviceServiceBinder;
78         Intent i = new Intent();
79 
80         if (provider == null) {
81             return;
82         }
83         if (key == null) {
84             setResult(RESULT_OK, i);
85             finish();
86         }
87         switch (key) {
88             case KEY_BLUETOOTH_TOGGLE:
89                 if (choice == YES) {
90                     if (AccessoryUtils.getDefaultBluetoothAdapter() != null) {
91                         AccessoryUtils.getDefaultBluetoothAdapter().disable();
92                     }
93                     i.putExtra(EXTRAS_DIRECTION, DIRECTION_BACK);
94                 }
95                 break;
96             case KEY_CONNECT:
97                 if (choice == YES) {
98                     provider.connectDevice(mDevice);
99                     i.putExtra(EXTRAS_DIRECTION, DIRECTION_BACK);
100                 }
101                 break;
102             case KEY_DISCONNECT:
103                 if (choice == YES) {
104                     provider.disconnectDevice(mDevice);
105                     i.putExtra(EXTRAS_DIRECTION, DIRECTION_BACK);
106                 }
107                 break;
108             case KEY_FORGET:
109                 if (choice == YES) {
110                     provider.forgetDevice(mDevice);
111                     i.putExtra(EXTRAS_DIRECTION, DIRECTION_BACK);
112                 }
113                 break;
114         }
115         setResult(RESULT_OK, i);
116         finish();
117     }
118 
119     @Override
onText(String key, String text)120     public void onText(String key, String text) {
121         BluetoothDeviceProvider provider = mBtDeviceServiceBinder;
122         if (KEY_RENAME.equals(key)) {
123             if (mDevice != null) {
124                 provider.renameDevice(mDevice, text);
125             }
126         }
127         Intent i = new Intent();
128         i.putExtra(EXTRAS_DIRECTION, DIRECTION_BACK);
129         setResult(RESULT_OK, i);
130         finish();
131     }
132 
133     @Override
onDestroy()134     public void onDestroy() {
135         if (mBtDeviceServiceBound) {
136             unbindService(mBtDeviceServiceConnection);
137         }
138         super.onDestroy();
139     }
140 }
141