/* * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.android.car.kitchensink.setting; import android.annotation.Nullable; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.hardware.usb.UsbDevice; import android.hardware.usb.UsbManager; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import com.google.android.car.kitchensink.R; import com.google.android.car.kitchensink.setting.usb.UsbDeviceSettings; import com.google.android.car.kitchensink.setting.usb.UsbHostController; import java.util.List; /** * Activity to handle USB device attached. *
* When user plugs in USB device:
* a) Device was used before and user selected handler for it.
* In this case handler will be launched.
* b) Device has not handler assigned. In this case supported handlers will be captured,
* and user will be presented with choice to assign default handler.
* After that handler will be lauched.
*/
public class UsbHostManagementActivity extends Activity
implements UsbHostController.UsbHostControllerCallbacks {
private static final String TAG = UsbHostManagementActivity.class.getSimpleName();
private static final boolean LOCAL_LOGD = true;
private static final boolean LOCAL_LOGV = true;
private HandlersAdapter mListAdapter;
private ListView mHandlersList;
private LinearLayout mProgressInfo;
private UsbHostController mController;
private PackageManager mPackageManager;
private final AdapterView.OnItemClickListener mHandlerClickListener =
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, final View view, int position, long id) {
UsbDeviceSettings settings = (UsbDeviceSettings) parent.getItemAtPosition(position);
mController.applyDeviceSettings(settings);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.usb_host);
mHandlersList = (ListView) findViewById(R.id.usb_handlers_list);
mProgressInfo = (LinearLayout) findViewById(R.id.usb_handlers_progress);
mListAdapter = new HandlersAdapter(this);
mHandlersList.setAdapter(mListAdapter);
mHandlersList.setOnItemClickListener(mHandlerClickListener);
mController = new UsbHostController(this, this);
mPackageManager = getPackageManager();
}
@Override
public void onDestroy() {
super.onDestroy();
mController.release();
}
@Override
public void onResume() {
super.onResume();
UsbDevice connectedDevice = getDevice();
if (connectedDevice != null) {
mController.processDevice(connectedDevice);
} else {
finish();
}
}
@Override
public void shutdown() {
runOnUiThread(new Runnable() {
@Override
public void run() {
finish();
}
});
}
@Override
public void processingStateChanged(final boolean processing) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mProgressInfo.setVisibility(processing ? View.VISIBLE : View.GONE);
}
});
}
@Override
public void titleChanged(String title) {
runOnUiThread(new Runnable() {
@Override
public void run() {
setTitle(title);
}
});
}
@Override
public void optionsUpdated(List