1 /* 2 * Copyright (C) 2014 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.example.android.wearable.flashlight; 18 19 import android.app.Activity; 20 import android.app.Fragment; 21 import android.app.FragmentManager; 22 import android.os.Bundle; 23 import android.support.v13.app.FragmentPagerAdapter; 24 import android.support.v4.view.ViewPager; 25 import android.support.v4.view.ViewPager.OnPageChangeListener; 26 import android.view.LayoutInflater; 27 import android.view.View; 28 import android.view.ViewGroup; 29 30 import java.util.ArrayList; 31 32 /** 33 * Let there be light. 34 */ 35 public class MainActivity extends Activity { 36 37 private ViewPager mViewPager; 38 39 @Override onCreate(Bundle savedInstanceState)40 protected void onCreate(Bundle savedInstanceState) { 41 super.onCreate(savedInstanceState); 42 43 setContentView(R.layout.main); 44 mViewPager = (ViewPager) findViewById(R.id.pager); 45 final LightFragmentAdapter adapter = new LightFragmentAdapter(getFragmentManager()); 46 adapter.addFragment(new WhiteLightFragment()); 47 final PartyLightFragment partyFragment = new PartyLightFragment(); 48 adapter.addFragment(partyFragment); 49 mViewPager.setAdapter(adapter); 50 mViewPager.setOnPageChangeListener(new OnPageChangeListener() { 51 52 @Override 53 public void onPageSelected(int position) { 54 if (position == 1) { 55 partyFragment.startCycling(); 56 } else { 57 partyFragment.stopCycling(); 58 } 59 } 60 61 @Override 62 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 63 } 64 65 @Override 66 public void onPageScrollStateChanged(int state) { 67 } 68 }); 69 } 70 71 static class LightFragmentAdapter extends FragmentPagerAdapter { 72 private ArrayList<Fragment> mFragments; 73 LightFragmentAdapter(FragmentManager fm)74 public LightFragmentAdapter(FragmentManager fm) { 75 super(fm); 76 mFragments = new ArrayList<Fragment>(); 77 } 78 79 @Override getItem(int position)80 public Fragment getItem(int position) { 81 return mFragments.get(position); 82 } 83 84 @Override getCount()85 public int getCount() { 86 return mFragments.size(); 87 } 88 addFragment(Fragment fragment)89 public void addFragment(Fragment fragment) { 90 mFragments.add(fragment); 91 // Update the pager when adding a fragment. 92 notifyDataSetChanged(); 93 } 94 } 95 96 public static class WhiteLightFragment extends Fragment { 97 98 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)99 public View onCreateView(LayoutInflater inflater, ViewGroup container, 100 Bundle savedInstanceState) { 101 return inflater.inflate(R.layout.white_light, container, false); 102 } 103 } 104 105 public static class PartyLightFragment extends Fragment { 106 107 private PartyLightView mView; 108 109 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)110 public View onCreateView(LayoutInflater inflater, ViewGroup container, 111 Bundle savedInstanceState) { 112 mView = (PartyLightView) inflater.inflate(R.layout.party_light, container, false); 113 return mView; 114 } 115 startCycling()116 public void startCycling() { 117 mView.startCycling(); 118 } 119 stopCycling()120 public void stopCycling() { 121 mView.stopCycling(); 122 } 123 124 } 125 } 126