1 /* 2 * Copyright (C) 2012 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.deskclock; 18 19 import android.app.Activity; 20 import android.app.Fragment; 21 import android.os.Bundle; 22 import android.support.v4.widget.PopupMenuCompat; 23 import android.view.MenuItem; 24 import android.view.View; 25 import android.widget.ImageButton; 26 import android.widget.PopupMenu; 27 28 public class DeskClockFragment extends Fragment { 29 30 protected ImageButton mFab; 31 protected ImageButton mLeftButton; 32 protected ImageButton mRightButton; 33 onPageChanged(int page)34 public void onPageChanged(int page) { 35 // Do nothing here , only in derived classes 36 } 37 onFabClick(View view)38 public void onFabClick(View view){ 39 // Do nothing here , only in derived classes 40 } 41 42 @Override onActivityCreated(Bundle savedInstanceState)43 public void onActivityCreated(Bundle savedInstanceState) { 44 super.onActivityCreated(savedInstanceState); 45 final Activity activity = getActivity(); 46 if (activity instanceof DeskClock) { 47 final DeskClock deskClockActivity = (DeskClock) activity; 48 mFab = deskClockActivity.getFab(); 49 mLeftButton = deskClockActivity.getLeftButton(); 50 mRightButton = deskClockActivity.getRightButton(); 51 } 52 } 53 setFabAppearance()54 public void setFabAppearance() { 55 // Do nothing here , only in derived classes 56 } 57 setLeftRightButtonAppearance()58 public void setLeftRightButtonAppearance() { 59 // Do nothing here , only in derived classes 60 } 61 onLeftButtonClick(View view)62 public void onLeftButtonClick(View view) { 63 // Do nothing here , only in derived classes 64 } 65 onRightButtonClick(View view)66 public void onRightButtonClick(View view) { 67 // Do nothing here , only in derived classes 68 } 69 /** 70 * Installs click and touch listeners on a fake overflow menu button. 71 * 72 * @param menuButton the fragment's fake overflow menu button 73 */ setupFakeOverflowMenuButton(View menuButton)74 public void setupFakeOverflowMenuButton(View menuButton) { 75 final PopupMenu fakeOverflow = new PopupMenu(menuButton.getContext(), menuButton) { 76 @Override 77 public void show() { 78 getActivity().onPrepareOptionsMenu(getMenu()); 79 super.show(); 80 } 81 }; 82 fakeOverflow.inflate(R.menu.desk_clock_menu); 83 fakeOverflow.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener () { 84 @Override 85 public boolean onMenuItemClick(MenuItem item) { 86 return getActivity().onOptionsItemSelected(item); 87 } 88 }); 89 90 menuButton.setOnTouchListener(PopupMenuCompat.getDragToOpenListener(fakeOverflow)); 91 menuButton.setOnClickListener(new View.OnClickListener() { 92 @Override 93 public void onClick(View v) { 94 fakeOverflow.show(); 95 } 96 }); 97 } 98 } 99