Home | History | Annotate | Download | only in shortcutlauncherdemo
      1 /*
      2  * Copyright (C) 2016 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 package com.example.android.pm.shortcutlauncherdemo;
     17 
     18 import android.app.ActionBar;
     19 import android.app.ActionBar.Tab;
     20 import android.app.ActionBar.TabListener;
     21 import android.app.Activity;
     22 import android.app.Fragment;
     23 import android.app.FragmentManager;
     24 import android.app.FragmentTransaction;
     25 import android.os.Bundle;
     26 import android.support.v13.app.FragmentStatePagerAdapter;
     27 import android.support.v4.os.BuildCompat;
     28 import android.support.v4.view.PagerAdapter;
     29 import android.support.v4.view.ViewPager;
     30 import android.view.ViewGroup;
     31 
     32 public class ShortcutLauncherMain extends Activity {
     33     private ViewPager mPager;
     34     private PagerAdapter mPagerAdapter;
     35 
     36     private int mNumTabs;
     37 
     38     @Override
     39     public void onCreate(Bundle savedInstanceState) {
     40         super.onCreate(savedInstanceState);
     41 
     42         setContentView(R.layout.main);
     43 
     44         mNumTabs = BuildCompat.isAtLeastO() ? 3 : 2;
     45 
     46         mPager = (ViewPager) findViewById(R.id.pager);
     47         mPager.setOffscreenPageLimit(2);
     48         mPagerAdapter = new MyPagerAdapter(getFragmentManager());
     49         mPager.setAdapter(mPagerAdapter);
     50 
     51         final ActionBar ab = getActionBar();
     52         ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
     53         ab.addTab(ab.newTab().setText("App list").setTabListener(mTabListener));
     54         ab.addTab(ab.newTab().setText("Pinned shortcuts").setTabListener(mTabListener));
     55         if (mNumTabs > 2) {
     56             ab.addTab(ab.newTab().setText("Create shortcuts").setTabListener(mTabListener));
     57         }
     58     }
     59 
     60     @Override
     61     public void onBackPressed() {
     62         // Ignore.
     63     }
     64 
     65     private TabListener mTabListener = new TabListener() {
     66         @Override
     67         public void onTabSelected(Tab tab, FragmentTransaction ft) {
     68             mPager.setCurrentItem(tab.getPosition());
     69         }
     70 
     71         @Override
     72         public void onTabUnselected(Tab tab, FragmentTransaction ft) {
     73 
     74         }
     75 
     76         @Override
     77         public void onTabReselected(Tab tab, FragmentTransaction ft) {
     78 
     79         }
     80     };
     81 
     82     private class MyPagerAdapter extends FragmentStatePagerAdapter {
     83         public MyPagerAdapter(FragmentManager fm) {
     84             super(fm);
     85         }
     86 
     87         @Override
     88         public Fragment getItem(int position) {
     89             switch (position) {
     90                 case 0:
     91                     return new AppListFragment();
     92                 case 1:
     93                     return new ShortcutListFragment().setArguments(
     94                             /* targetPackage =*/ null,
     95                             /* targetActivity =*/ null,
     96                             /* includeDynamic = */ false,
     97                             /* includeManifest = */ false,
     98                             /* includePinned =*/ true,
     99                             null /* means "all profiles" of this user*/,
    100                             /* showDetails =*/ true
    101                             );
    102                 case 2:
    103                     return new ShortcutTemplateListFragment();
    104             }
    105             return null;
    106         }
    107 
    108         @Override
    109         public int getCount() {
    110             return mNumTabs;
    111         }
    112 
    113         @Override
    114         public void setPrimaryItem(ViewGroup container, int position, Object object) {
    115             super.setPrimaryItem(container, position, object);
    116 
    117             getActionBar().selectTab(getActionBar().getTabAt(position));
    118         }
    119     }
    120 }
    121