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