| /* |
| * Copyright (C) 2013 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package com.android.photos; |
| |
| import android.app.ActionBar; |
| import android.app.ActionBar.Tab; |
| import android.app.Activity; |
| import android.app.Fragment; |
| import android.app.FragmentTransaction; |
| import android.content.Intent; |
| import android.os.Bundle; |
| import android.view.Menu; |
| import android.view.MenuItem; |
| |
| import com.android.camera.CameraActivity; |
| import com.android.gallery3d.R; |
| |
| public class GalleryActivity extends Activity { |
| |
| private final String FTAG_PHOTOSET = "PhotoSet"; |
| private final String FTAG_ALBUMSET = "AlbumSet"; |
| |
| @Override |
| protected void onCreate(Bundle savedInstanceState) { |
| super.onCreate(savedInstanceState); |
| |
| setupActionBar(); |
| } |
| |
| private void setupActionBar() { |
| ActionBar ab = getActionBar(); |
| ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); |
| ab.setDisplayShowHomeEnabled(false); |
| ab.setDisplayShowTitleEnabled(false); |
| Tab tab = ab.newTab(); |
| tab.setText(R.string.tab_photos); |
| tab.setTabListener(new TabListener<PhotoSetFragment>(this, |
| FTAG_PHOTOSET, PhotoSetFragment.class)); |
| ab.addTab(tab, true); |
| tab = ab.newTab(); |
| tab.setText(R.string.tab_albums); |
| tab.setTabListener(new TabListener<AlbumSetFragment>(this, |
| FTAG_ALBUMSET, AlbumSetFragment.class)); |
| ab.addTab(tab); |
| } |
| |
| @Override |
| public boolean onCreateOptionsMenu(Menu menu) { |
| getMenuInflater().inflate(R.menu.gallery, menu); |
| return true; |
| } |
| |
| @Override |
| public boolean onOptionsItemSelected(MenuItem item) { |
| switch (item.getItemId()) { |
| case R.id.menu_camera: |
| Intent intent = new Intent(this, CameraActivity.class); |
| intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| startActivity(intent); |
| return true; |
| default: |
| return super.onOptionsItemSelected(item); |
| } |
| } |
| |
| private static class TabListener<T extends Fragment> implements ActionBar.TabListener { |
| private Fragment mFragment; |
| private final Activity mActivity; |
| private final String mTag; |
| private final Class<T> mClass; |
| |
| /** Constructor used each time a new tab is created. |
| * @param activity The host Activity, used to instantiate the fragment |
| * @param tag The identifier tag for the fragment |
| * @param clz The fragment's Class, used to instantiate the fragment |
| */ |
| public TabListener(Activity activity, String tag, Class<T> clz) { |
| mActivity = activity; |
| mTag = tag; |
| mClass = clz; |
| } |
| |
| /* The following are each of the ActionBar.TabListener callbacks */ |
| |
| @Override |
| public void onTabSelected(Tab tab, FragmentTransaction ft) { |
| // Check if the fragment is already initialized |
| if (mFragment == null) { |
| // If not, instantiate and add it to the activity |
| mFragment = Fragment.instantiate(mActivity, mClass.getName()); |
| ft.add(android.R.id.content, mFragment, mTag); |
| } else { |
| // If it exists, simply attach it in order to show it |
| ft.attach(mFragment); |
| } |
| } |
| |
| @Override |
| public void onTabUnselected(Tab tab, FragmentTransaction ft) { |
| if (mFragment != null) { |
| // Detach the fragment, because another one is being attached |
| ft.detach(mFragment); |
| } |
| } |
| |
| @Override |
| public void onTabReselected(Tab tab, FragmentTransaction ft) { |
| // User selected the already selected tab. Usually do nothing. |
| } |
| } |
| } |