OSDN Git Service

46b5140fba6ff9624b772ff0773c8052964e7382
[android-x86/packages-apps-Gallery2.git] / src / com / android / photos / GalleryActivity.java
1 /*
2  * Copyright (C) 2013 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.photos;
18
19 import android.app.ActionBar;
20 import android.app.ActionBar.Tab;
21 import android.app.Activity;
22 import android.app.Fragment;
23 import android.app.FragmentTransaction;
24 import android.content.Intent;
25 import android.os.Bundle;
26 import android.view.Menu;
27 import android.view.MenuItem;
28
29 import com.android.camera.CameraActivity;
30 import com.android.gallery3d.R;
31
32 public class GalleryActivity extends Activity {
33
34     private final String FTAG_PHOTOSET = "PhotoSet";
35     private final String FTAG_ALBUMSET = "AlbumSet";
36
37     @Override
38     protected void onCreate(Bundle savedInstanceState) {
39         super.onCreate(savedInstanceState);
40
41         setupActionBar();
42     }
43
44     private void setupActionBar() {
45         ActionBar ab = getActionBar();
46         ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
47         ab.setDisplayShowHomeEnabled(false);
48         ab.setDisplayShowTitleEnabled(false);
49         Tab tab = ab.newTab();
50         tab.setText(R.string.tab_photos);
51         tab.setTabListener(new TabListener<PhotoSetFragment>(this,
52                 FTAG_PHOTOSET, PhotoSetFragment.class));
53         ab.addTab(tab, true);
54         tab = ab.newTab();
55         tab.setText(R.string.tab_albums);
56         tab.setTabListener(new TabListener<AlbumSetFragment>(this,
57                 FTAG_ALBUMSET, AlbumSetFragment.class));
58         ab.addTab(tab);
59     }
60
61     @Override
62     public boolean onCreateOptionsMenu(Menu menu) {
63         getMenuInflater().inflate(R.menu.gallery, menu);
64         return true;
65     }
66
67     @Override
68     public boolean onOptionsItemSelected(MenuItem item) {
69         switch (item.getItemId()) {
70         case R.id.menu_camera:
71             Intent intent = new Intent(this, CameraActivity.class);
72             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
73             startActivity(intent);
74             return true;
75         default:
76             return super.onOptionsItemSelected(item);
77         }
78     }
79
80     private static class TabListener<T extends Fragment> implements ActionBar.TabListener {
81         private Fragment mFragment;
82         private final Activity mActivity;
83         private final String mTag;
84         private final Class<T> mClass;
85
86         /** Constructor used each time a new tab is created.
87           * @param activity  The host Activity, used to instantiate the fragment
88           * @param tag  The identifier tag for the fragment
89           * @param clz  The fragment's Class, used to instantiate the fragment
90           */
91         public TabListener(Activity activity, String tag, Class<T> clz) {
92             mActivity = activity;
93             mTag = tag;
94             mClass = clz;
95         }
96
97         /* The following are each of the ActionBar.TabListener callbacks */
98
99         @Override
100         public void onTabSelected(Tab tab, FragmentTransaction ft) {
101             // Check if the fragment is already initialized
102             if (mFragment == null) {
103                 // If not, instantiate and add it to the activity
104                 mFragment = Fragment.instantiate(mActivity, mClass.getName());
105                 ft.add(android.R.id.content, mFragment, mTag);
106             } else {
107                 // If it exists, simply attach it in order to show it
108                 ft.attach(mFragment);
109             }
110         }
111
112         @Override
113         public void onTabUnselected(Tab tab, FragmentTransaction ft) {
114             if (mFragment != null) {
115                 // Detach the fragment, because another one is being attached
116                 ft.detach(mFragment);
117             }
118         }
119
120         @Override
121         public void onTabReselected(Tab tab, FragmentTransaction ft) {
122             // User selected the already selected tab. Usually do nothing.
123         }
124     }
125 }