DeviceDisconnectedReceiver.java revision 1d5dedc9e44fdccf0636cedac90529c0c5f6e166
1/* 2 * Copyright (C) 2010 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 17package com.android.camerabrowser; 18 19import android.app.Activity; 20import android.content.BroadcastReceiver; 21import android.content.Context; 22import android.content.Intent; 23import android.content.IntentFilter; 24import android.hardware.Usb; 25import android.net.Uri; 26 27public class DeviceDisconnectedReceiver extends BroadcastReceiver { 28 29 private final Activity mActivity; 30 private final int mDeviceID; 31 32 public DeviceDisconnectedReceiver(Activity activity, int deviceID) { 33 mActivity = activity; 34 mDeviceID = deviceID; 35 36 IntentFilter filter = new IntentFilter(Usb.ACTION_USB_CAMERA_DETACHED); 37 filter.addDataScheme("content"); 38 activity.registerReceiver(this, filter); 39 } 40 41 @Override 42 public void onReceive(Context context, Intent intent) { 43 // close our activity if the device it is displaying is disconnected 44 Uri uri = intent.getData(); 45 int id = Integer.parseInt(uri.getPathSegments().get(1)); 46 if (id == mDeviceID) { 47 mActivity.finish(); 48 } 49 } 50}