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.annotation.TargetApi;
20 import android.app.Activity;
21 import android.content.Intent;
22 import android.content.res.AssetFileDescriptor;
23 import android.net.Uri;
24 import android.os.Build;
25 import android.os.Bundle;
26 import android.util.Log;
27 import android.widget.Toast;
28 
29 import com.android.gallery3d.R;
30 import com.android.gallery3d.app.GalleryActivity;
31 import com.android.gallery3d.app.PhotoPage;
32 import com.android.gallery3d.common.ApiHelper;
33 
34 public class WidgetClickHandler extends Activity {
35     private static final String TAG = "PhotoAppWidgetClickHandler";
36 
isValidDataUri(Uri dataUri)37     private boolean isValidDataUri(Uri dataUri) {
38         if (dataUri == null) return false;
39         try {
40             AssetFileDescriptor f = getContentResolver()
41                     .openAssetFileDescriptor(dataUri, "r");
42             f.close();
43             return true;
44         } catch (Throwable e) {
45             Log.w(TAG, "cannot open uri: " + dataUri, e);
46             return false;
47         }
48     }
49 
50     @Override
51     @TargetApi(ApiHelper.VERSION_CODES.HONEYCOMB)
onCreate(Bundle savedState)52     protected void onCreate(Bundle savedState) {
53         super.onCreate(savedState);
54         // The behavior is changed in JB, refer to b/6384492 for more details
55         boolean tediousBack = Build.VERSION.SDK_INT >= ApiHelper.VERSION_CODES.JELLY_BEAN;
56         Uri uri = getIntent().getData();
57         Intent intent;
58         if (isValidDataUri(uri)) {
59             intent = new Intent(Intent.ACTION_VIEW, uri);
60             if (tediousBack) {
61                 intent.putExtra(PhotoPage.KEY_TREAT_BACK_AS_UP, true);
62             }
63         } else {
64             Toast.makeText(this,
65                     R.string.no_such_item, Toast.LENGTH_LONG).show();
66             intent = new Intent(this, GalleryActivity.class);
67         }
68         if (tediousBack) {
69             intent.setFlags(
70                     Intent.FLAG_ACTIVITY_NEW_TASK |
71                     Intent.FLAG_ACTIVITY_CLEAR_TASK |
72                     Intent.FLAG_ACTIVITY_TASK_ON_HOME);
73         }
74         startActivity(intent);
75         finish();
76     }
77 }
78