2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.photos;
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;
29 import com.android.camera.CameraActivity;
30 import com.android.gallery3d.R;
32 public class GalleryActivity extends Activity {
34 private final String FTAG_PHOTOSET = "PhotoSet";
35 private final String FTAG_ALBUMSET = "AlbumSet";
38 protected void onCreate(Bundle savedInstanceState) {
39 super.onCreate(savedInstanceState);
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));
55 tab.setText(R.string.tab_albums);
56 tab.setTabListener(new TabListener<AlbumSetFragment>(this,
57 FTAG_ALBUMSET, AlbumSetFragment.class));
62 public boolean onCreateOptionsMenu(Menu menu) {
63 getMenuInflater().inflate(R.menu.gallery, menu);
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);
76 return super.onOptionsItemSelected(item);
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;
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
91 public TabListener(Activity activity, String tag, Class<T> clz) {
97 /* The following are each of the ActionBar.TabListener callbacks */
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);
107 // If it exists, simply attach it in order to show it
108 ft.attach(mFragment);
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);
121 public void onTabReselected(Tab tab, FragmentTransaction ft) {
122 // User selected the already selected tab. Usually do nothing.