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