1 /* 2 * Copyright (C) 2021 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.google.android.tv.btservices.settings; 18 19 import android.app.Activity; 20 import android.app.Fragment; 21 import android.app.FragmentManager; 22 import android.app.FragmentTransaction; 23 import android.content.ComponentName; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.content.ServiceConnection; 27 import android.os.Bundle; 28 import android.os.Handler; 29 import android.os.IBinder; 30 import com.google.android.tv.btservices.BluetoothDeviceService; 31 import com.google.android.tv.btservices.BluetoothUtils; 32 import com.google.android.tv.btservices.R; 33 import com.google.android.tv.btservices.SimplifiedConnection; 34 35 public class RemoteDfuConfirmationActivity extends Activity implements ResponseFragment.Listener { 36 37 public static final String EXTRA_BT_ADDRESS = "extra_bt_address"; 38 public static final String EXTRA_BT_NAME= "extra_bt_name"; 39 40 private static final String KEY_UPDATE = "update"; 41 private static final int CONTINUE = R.string.settings_continue; 42 private static final int CANCEL = R.string.settings_cancel; 43 private static final int[] CONT_CANCEL_ARGS = {CONTINUE, CANCEL}; 44 private static final String FRAGMENT_TAG = "dfu_confirm"; 45 46 private final Handler mHandler = new Handler(); 47 private boolean mBtDeviceServiceBound; 48 private BluetoothDeviceService.LocalBinder mBtDeviceServiceBinder; 49 50 private final ServiceConnection mBtDeviceServiceConnection = new SimplifiedConnection() { 51 52 @Override 53 protected void cleanUp() { 54 mBtDeviceServiceBound = false; 55 mBtDeviceServiceBinder = null; 56 } 57 58 @Override 59 public void onServiceConnected(ComponentName className, IBinder service) { 60 mBtDeviceServiceBinder = (BluetoothDeviceService.LocalBinder) service; 61 mBtDeviceServiceBound = true; 62 mHandler.post(RemoteDfuConfirmationActivity.this::show); 63 } 64 }; 65 show()66 private void show() { 67 FragmentManager fm = getFragmentManager(); 68 Fragment fragment = fm.findFragmentByTag(FRAGMENT_TAG); 69 if (fragment == null) { 70 fragment = new ResponseFragment(); 71 Bundle args = new Bundle(); 72 ResponseFragment.prepareArgs( 73 args, 74 KEY_UPDATE, 75 R.string.settings_bt_update, 76 R.string.settings_bt_update_summary, 77 0, 78 CONT_CANCEL_ARGS, 79 null, 80 ResponseFragment.DEFAULT_CHOICE_UNDEFINED 81 ); 82 fragment.setArguments(args); 83 FragmentTransaction transact = fm.beginTransaction(); 84 transact.add(android.R.id.content, fragment, FRAGMENT_TAG); 85 transact.commit(); 86 } 87 } 88 89 @Override onCreate(Bundle savedInstanceState)90 protected void onCreate(Bundle savedInstanceState) { 91 super.onCreate(savedInstanceState); 92 bindService(new Intent(this, BluetoothUtils.getBluetoothDeviceServiceClass(this)), 93 mBtDeviceServiceConnection, Context.BIND_AUTO_CREATE); 94 } 95 96 @Override onChoice(String key, int choice)97 public void onChoice(String key, int choice) { 98 final Intent origIntent = getIntent(); 99 final String address = origIntent.getStringExtra(EXTRA_BT_ADDRESS); 100 if (choice == CONTINUE) { 101 Intent intent = new Intent(this, RemoteDfuActivity.class); 102 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 103 intent.putExtra(EXTRA_BT_ADDRESS, address); 104 startActivity(intent); 105 finish(); 106 } else if (choice == CANCEL) { 107 if (mBtDeviceServiceBinder != null) { 108 mBtDeviceServiceBinder.dismissDfuNotification(address); 109 } 110 finish(); 111 } 112 } 113 114 @Override onText(String key, String text)115 public void onText(String key, String text) {} 116 117 @Override onDestroy()118 public void onDestroy() { 119 if (mBtDeviceServiceBound) { 120 unbindService(mBtDeviceServiceConnection); 121 } 122 super.onDestroy(); 123 } 124 } 125