1 /* 2 * Copyright (C) 2009 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.gallery3d.gadget; 18 19 import android.app.Activity; 20 import android.content.Intent; 21 import android.content.res.AssetFileDescriptor; 22 import android.net.Uri; 23 import android.os.Bundle; 24 import android.util.Log; 25 import android.widget.Toast; 26 27 import com.android.gallery3d.R; 28 import com.android.gallery3d.app.Gallery; 29 import com.android.gallery3d.app.PhotoPage; 30 31 public class WidgetClickHandler extends Activity { 32 private static final String TAG = "PhotoAppWidgetClickHandler"; 33 34 private boolean isValidDataUri(Uri dataUri) { 35 if (dataUri == null) return false; 36 try { 37 AssetFileDescriptor f = getContentResolver() 38 .openAssetFileDescriptor(dataUri, "r"); 39 f.close(); 40 return true; 41 } catch (Throwable e) { 42 Log.w(TAG, "cannot open uri: " + dataUri, e); 43 return false; 44 } 45 } 46 47 @Override 48 protected void onCreate(Bundle savedState) { 49 super.onCreate(savedState); 50 Uri uri = getIntent().getData(); 51 Intent intent; 52 if (isValidDataUri(uri)) { 53 intent = new Intent(Intent.ACTION_VIEW, uri); 54 intent.putExtra(PhotoPage.KEY_TREAT_BACK_AS_UP, true); 55 } else { 56 Toast.makeText(this, 57 R.string.no_such_item, Toast.LENGTH_LONG).show(); 58 intent = new Intent(this, Gallery.class); 59 } 60 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | 61 Intent.FLAG_ACTIVITY_TASK_ON_HOME); 62 startActivity(intent); 63 finish(); 64 } 65 } 66