1 /* 2 * Copyright (C) 2016 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 package com.google.android.car.kitchensink.setting; 17 18 import android.annotation.Nullable; 19 import android.app.Activity; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.pm.ApplicationInfo; 24 import android.content.pm.PackageManager; 25 import android.content.pm.PackageManager.NameNotFoundException; 26 import android.hardware.usb.UsbDevice; 27 import android.hardware.usb.UsbManager; 28 import android.os.Bundle; 29 import android.util.Log; 30 import android.view.View; 31 import android.view.ViewGroup; 32 import android.widget.AdapterView; 33 import android.widget.ArrayAdapter; 34 import android.widget.ImageView; 35 import android.widget.LinearLayout; 36 import android.widget.ListView; 37 import android.widget.TextView; 38 39 import com.google.android.car.kitchensink.R; 40 import com.google.android.car.kitchensink.setting.usb.UsbDeviceSettings; 41 import com.google.android.car.kitchensink.setting.usb.UsbHostController; 42 43 import java.util.List; 44 45 /** 46 * Activity to handle USB device attached. 47 * <p> 48 * When user plugs in USB device: 49 * a) Device was used before and user selected handler for it. 50 * In this case handler will be launched. 51 * b) Device has not handler assigned. In this case supported handlers will be captured, 52 * and user will be presented with choice to assign default handler. 53 * After that handler will be lauched. 54 */ 55 public class UsbHostManagementActivity extends Activity 56 implements UsbHostController.UsbHostControllerCallbacks { 57 private static final String TAG = UsbHostManagementActivity.class.getSimpleName(); 58 private static final boolean LOCAL_LOGD = true; 59 private static final boolean LOCAL_LOGV = true; 60 61 private HandlersAdapter mListAdapter; 62 private ListView mHandlersList; 63 private LinearLayout mProgressInfo; 64 private UsbHostController mController; 65 private PackageManager mPackageManager; 66 67 private final AdapterView.OnItemClickListener mHandlerClickListener = 68 new AdapterView.OnItemClickListener() { 69 @Override 70 public void onItemClick(AdapterView<?> parent, final View view, int position, long id) { 71 UsbDeviceSettings settings = (UsbDeviceSettings) parent.getItemAtPosition(position); 72 mController.applyDeviceSettings(settings); 73 } 74 }; 75 76 @Override 77 public void onCreate(Bundle savedInstanceState) { 78 super.onCreate(savedInstanceState); 79 setContentView(R.layout.usb_host); 80 mHandlersList = (ListView) findViewById(R.id.usb_handlers_list); 81 mProgressInfo = (LinearLayout) findViewById(R.id.usb_handlers_progress); 82 mListAdapter = new HandlersAdapter(this); 83 mHandlersList.setAdapter(mListAdapter); 84 mHandlersList.setOnItemClickListener(mHandlerClickListener); 85 mController = new UsbHostController(this, this); 86 mPackageManager = getPackageManager(); 87 } 88 89 @Override 90 public void onDestroy() { 91 super.onDestroy(); 92 mController.release(); 93 } 94 95 @Override 96 public void onResume() { 97 super.onResume(); 98 UsbDevice connectedDevice = getDevice(); 99 if (connectedDevice != null) { 100 mController.processDevice(connectedDevice); 101 } else { 102 finish(); 103 } 104 } 105 106 @Override 107 public void shutdown() { 108 runOnUiThread(new Runnable() { 109 @Override 110 public void run() { 111 finish(); 112 } 113 }); 114 } 115 116 @Override 117 public void processingStateChanged(final boolean processing) { 118 runOnUiThread(new Runnable() { 119 @Override 120 public void run() { 121 mProgressInfo.setVisibility(processing ? View.VISIBLE : View.GONE); 122 } 123 }); 124 } 125 126 @Override 127 public void titleChanged(String title) { 128 runOnUiThread(new Runnable() { 129 @Override 130 public void run() { 131 setTitle(title); 132 } 133 }); 134 } 135 136 @Override 137 public void optionsUpdated(List<UsbDeviceSettings> options) { 138 runOnUiThread(new Runnable() { 139 @Override 140 public void run() { 141 mListAdapter.clear(); 142 mListAdapter.addAll(options); 143 } 144 }); 145 } 146 147 148 @Override 149 public void onNewIntent(Intent intent) { 150 super.onNewIntent(intent); 151 setIntent(intent); 152 } 153 154 @Nullable 155 private UsbDevice getDevice() { 156 if (!UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(getIntent().getAction())) { 157 return null; 158 } 159 return (UsbDevice) getIntent().getParcelableExtra(UsbManager.EXTRA_DEVICE); 160 } 161 162 private static Intent createDeviceAttachedIntent(UsbDevice device) { 163 Intent intent = new Intent(UsbManager.ACTION_USB_DEVICE_ATTACHED); 164 intent.putExtra(UsbManager.EXTRA_DEVICE, device); 165 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 166 return intent; 167 } 168 169 private class HandlersAdapter extends ArrayAdapter<UsbDeviceSettings> { 170 class HandlerHolder { 171 public TextView mAppName; 172 public ImageView mAppIcon; 173 } 174 175 HandlersAdapter(Context context) { 176 super(context, R.layout.usb_handler_row); 177 } 178 179 @Override 180 public View getView(int position, View convertView, ViewGroup parent) { 181 View rowView = convertView; 182 if (rowView == null) { 183 rowView = getLayoutInflater().inflate(R.layout.usb_handler_row, null); 184 HandlerHolder holder = new HandlerHolder(); 185 holder.mAppName = (TextView) rowView.findViewById(R.id.usb_handler_title); 186 holder.mAppIcon = (ImageView) rowView.findViewById(R.id.usb_handler_icon); 187 rowView.setTag(holder); 188 } 189 190 HandlerHolder holder = (HandlerHolder) rowView.getTag(); 191 ComponentName handler = getItem(position).getHandler(); 192 193 try { 194 ApplicationInfo appInfo = 195 mPackageManager.getApplicationInfo(handler.getPackageName(), 0); 196 holder.mAppName.setText(appInfo.loadLabel(mPackageManager)); 197 holder.mAppIcon.setImageDrawable(appInfo.loadIcon(mPackageManager)); 198 } catch (NameNotFoundException e) { 199 Log.e(TAG, "Handling package not found: " + handler.getPackageName()); 200 holder.mAppName.setText(handler.flattenToShortString()); 201 holder.mAppIcon.setImageResource(android.R.color.transparent); 202 } 203 return rowView; 204 } 205 } 206 } 207