1 /* 2 * Copyright (C) 2017 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.companiondevicemanager; 18 19 import static android.companion.BluetoothDeviceFilterUtils.getDeviceMacAddress; 20 21 import android.app.Activity; 22 import android.companion.CompanionDeviceManager; 23 import android.content.Intent; 24 import android.content.pm.PackageManager; 25 import android.content.res.Resources; 26 import android.database.DataSetObserver; 27 import android.os.Bundle; 28 import android.text.Html; 29 import android.util.Log; 30 import android.view.Gravity; 31 import android.view.View; 32 import android.widget.ListView; 33 import android.widget.ProgressBar; 34 import android.widget.TextView; 35 36 import com.android.companiondevicemanager.DeviceDiscoveryService.DeviceFilterPair; 37 import com.android.internal.util.Preconditions; 38 39 public class DeviceChooserActivity extends Activity { 40 41 private static final boolean DEBUG = false; 42 private static final String LOG_TAG = "DeviceChooserActivity"; 43 44 private ListView mDeviceListView; 45 private View mPairButton; 46 private View mCancelButton; 47 48 @Override onCreate(Bundle savedInstanceState)49 public void onCreate(Bundle savedInstanceState) { 50 super.onCreate(savedInstanceState); 51 52 if (DEBUG) Log.i(LOG_TAG, "Started with intent " + getIntent()); 53 54 if (getService().mDevicesFound.isEmpty()) { 55 Log.e(LOG_TAG, "About to show UI, but no devices to show"); 56 } 57 58 if (getService().mRequest.isSingleDevice()) { 59 setContentView(R.layout.device_confirmation); 60 final DeviceFilterPair selectedDevice = getService().mDevicesFound.get(0); 61 setTitle(Html.fromHtml(getString( 62 R.string.confirmation_title, 63 getCallingAppName(), 64 selectedDevice.getDisplayName()), 0)); 65 getService().mSelectedDevice = selectedDevice; 66 } else { 67 setContentView(R.layout.device_chooser); 68 setTitle(Html.fromHtml(getString(R.string.chooser_title, getCallingAppName()), 0)); 69 mDeviceListView = findViewById(R.id.device_list); 70 final DeviceDiscoveryService.DevicesAdapter adapter = getService().mDevicesAdapter; 71 mDeviceListView.setAdapter(adapter); 72 adapter.registerDataSetObserver(new DataSetObserver() { 73 @Override 74 public void onChanged() { 75 updatePairButtonEnabled(); 76 } 77 }); 78 mDeviceListView.addFooterView(getProgressBar(), null, false); 79 } 80 81 mPairButton = findViewById(R.id.button_pair); 82 mPairButton.setOnClickListener(v -> onPairTapped(getService().mSelectedDevice)); 83 updatePairButtonEnabled(); 84 85 mCancelButton = findViewById(R.id.button_cancel); 86 mCancelButton.setOnClickListener(v -> cancel()); 87 } 88 cancel()89 private void cancel() { 90 getService().onCancel(); 91 setResult(RESULT_CANCELED); 92 finish(); 93 } 94 95 @Override onStop()96 protected void onStop() { 97 super.onStop(); 98 if (!isFinishing() && !isChangingConfigurations()) { 99 cancel(); 100 } 101 } 102 getCallingAppName()103 private CharSequence getCallingAppName() { 104 try { 105 final PackageManager packageManager = getPackageManager(); 106 String callingPackage = Preconditions.checkStringNotEmpty( 107 getCallingPackage(), 108 "This activity must be called for result"); 109 return packageManager.getApplicationLabel( 110 packageManager.getApplicationInfo(callingPackage, 0)); 111 } catch (PackageManager.NameNotFoundException e) { 112 throw new RuntimeException(e); 113 } 114 } 115 116 @Override setTitle(CharSequence title)117 public void setTitle(CharSequence title) { 118 final TextView titleView = findViewById(R.id.title); 119 final int padding = getPadding(getResources()); 120 titleView.setPadding(padding, padding, padding, padding); 121 titleView.setText(title); 122 } 123 124 //TODO put in resources xmls getProgressBar()125 private ProgressBar getProgressBar() { 126 final ProgressBar progressBar = new ProgressBar(this); 127 progressBar.setForegroundGravity(Gravity.CENTER_HORIZONTAL); 128 final int padding = getPadding(getResources()); 129 progressBar.setPadding(padding, padding, padding, padding); 130 return progressBar; 131 } 132 getPadding(Resources r)133 static int getPadding(Resources r) { 134 return r.getDimensionPixelSize(R.dimen.padding); 135 } 136 updatePairButtonEnabled()137 private void updatePairButtonEnabled() { 138 mPairButton.setEnabled(getService().mSelectedDevice != null); 139 } 140 getService()141 private DeviceDiscoveryService getService() { 142 return DeviceDiscoveryService.sInstance; 143 } 144 onPairTapped(DeviceFilterPair selectedDevice)145 protected void onPairTapped(DeviceFilterPair selectedDevice) { 146 getService().onDeviceSelected( 147 getCallingPackage(), getDeviceMacAddress(selectedDevice.device)); 148 setResult(RESULT_OK, 149 new Intent().putExtra(CompanionDeviceManager.EXTRA_DEVICE, selectedDevice.device)); 150 finish(); 151 } 152 }