OSDN Git Service

51c78d64d55a41a207486d9d0d0431373d247f40
[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 androidx.legacy.app.FragmentPagerAdapter;
27 import androidx.viewpager.widget.ViewPager;
28 import android.view.Menu;
29 import android.view.MenuItem;
30 import android.view.ViewGroup;
31
32 import com.android.gallery3d.R;
33
34 import java.util.ArrayList;
35
36 public class GalleryActivity extends Activity implements MultiChoiceManager.Provider {
37
38     private MultiChoiceManager mMultiChoiceManager;
39     private ViewPager mViewPager;
40     private TabsAdapter mTabsAdapter;
41
42     @Override
43     protected void onCreate(Bundle savedInstanceState) {
44         super.onCreate(savedInstanceState);
45         mMultiChoiceManager = new MultiChoiceManager(this);
46         mViewPager = new ViewPager(this);
47         mViewPager.setId(R.id.viewpager);
48         setContentView(mViewPager);
49
50         ActionBar ab = getActionBar();
51         ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
52         ab.setDisplayShowHomeEnabled(false);
53         ab.setDisplayShowTitleEnabled(false);
54
55         mTabsAdapter = new TabsAdapter(this, mViewPager);
56         mTabsAdapter.addTab(ab.newTab().setText(R.string.tab_photos),
57                 PhotoSetFragment.class, null);
58         mTabsAdapter.addTab(ab.newTab().setText(R.string.tab_albums),
59                 AlbumSetFragment.class, null);
60
61         if (savedInstanceState != null) {
62             ab.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
63         }
64     }
65
66     @Override
67     protected void onSaveInstanceState(Bundle outState) {
68         super.onSaveInstanceState(outState);
69         outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
70     }
71
72     @Override
73     public boolean onCreateOptionsMenu(Menu menu) {
74         getMenuInflater().inflate(R.menu.gallery, menu);
75         return true;
76     }
77
78     @Override
79     public boolean onOptionsItemSelected(MenuItem item) {
80         switch (item.getItemId()) {
81         case R.id.menu_camera:
82             // TODO: Call the correct Camera intent.
83             throw new RuntimeException("Not implemented yet.");
84 //            Intent intent = new Intent(this, CameraActivity.class);
85 //            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
86 //            startActivity(intent);
87 //            return true;
88         default:
89             return super.onOptionsItemSelected(item);
90         }
91     }
92
93     public static class TabsAdapter extends FragmentPagerAdapter implements
94             ActionBar.TabListener, ViewPager.OnPageChangeListener {
95
96         private final GalleryActivity mActivity;
97         private final ActionBar mActionBar;
98         private final ViewPager mViewPager;
99         private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
100
101         static final class TabInfo {
102
103             private final Class<?> clss;
104             private final Bundle args;
105
106             TabInfo(Class<?> _class, Bundle _args) {
107                 clss = _class;
108                 args = _args;
109             }
110         }
111
112         public TabsAdapter(GalleryActivity activity, ViewPager pager) {
113             super(activity.getFragmentManager());
114             mActivity = activity;
115             mActionBar = activity.getActionBar();
116             mViewPager = pager;
117             mViewPager.setAdapter(this);
118             mViewPager.setOnPageChangeListener(this);
119         }
120
121         public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
122             TabInfo info = new TabInfo(clss, args);
123             tab.setTag(info);
124             tab.setTabListener(this);
125             mTabs.add(info);
126             mActionBar.addTab(tab);
127             notifyDataSetChanged();
128         }
129
130         @Override
131         public int getCount() {
132             return mTabs.size();
133         }
134
135         @Override
136         public Fragment getItem(int position) {
137             TabInfo info = mTabs.get(position);
138             return Fragment.instantiate(mActivity, info.clss.getName(),
139                     info.args);
140         }
141
142         @Override
143         public void onPageScrolled(int position, float positionOffset,
144                 int positionOffsetPixels) {
145         }
146
147         @Override
148         public void onPageSelected(int position) {
149             mActionBar.setSelectedNavigationItem(position);
150         }
151
152         @Override
153         public void setPrimaryItem(ViewGroup container, int position, Object object) {
154             super.setPrimaryItem(container, position, object);
155             mActivity.mMultiChoiceManager.setDelegate((MultiChoiceManager.Delegate) object);
156         }
157
158         @Override
159         public void onPageScrollStateChanged(int state) {
160         }
161
162         @Override
163         public void onTabSelected(Tab tab, FragmentTransaction ft) {
164             Object tag = tab.getTag();
165             for (int i = 0; i < mTabs.size(); i++) {
166                 if (mTabs.get(i) == tag) {
167                     mViewPager.setCurrentItem(i);
168                 }
169             }
170         }
171
172         @Override
173         public void onTabUnselected(Tab tab, FragmentTransaction ft) {
174         }
175
176         @Override
177         public void onTabReselected(Tab tab, FragmentTransaction ft) {
178         }
179     }
180
181     @Override
182     public MultiChoiceManager getMultiChoiceManager() {
183         return mMultiChoiceManager;
184     }
185 }