AccountServerBaseFragment.java revision 55110ca1ad8ce48a5429f9f351d013691c10b806
1/* 2 * Copyright (C) 2010 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 17package com.android.email.activity.setup; 18 19import com.android.email.R; 20 21import android.app.Activity; 22import android.app.Fragment; 23import android.content.Context; 24import android.os.Bundle; 25import android.view.Menu; 26import android.view.MenuInflater; 27import android.view.MenuItem; 28 29/** 30 * Common base class for server settings fragments, so they can be more easily manipulated by 31 * AccountSettingsXL. Provides the following common functionality: 32 * 33 * Activity-provided callbacks 34 * Activity callback during onAttach 35 * Present "Next" button and respond to its clicks 36 */ 37public abstract class AccountServerBaseFragment extends Fragment { 38 39 protected Context mContext; 40 protected Callback mCallback = EmptyCallback.INSTANCE; 41 protected boolean mNextButtonEnabled; 42 43 /** 44 * Callback interface that owning activities must provide 45 */ 46 public interface Callback { 47 /** 48 * Called each time the user-entered input transitions between valid and invalid 49 * @param enable true to enable proceed/next button, false to disable 50 */ 51 public void onEnableProceedButtons(boolean enable); 52 /** 53 * Called when user clicks "next" 54 * @param checkMode values from {@link SetupData} 55 */ 56 public void onProceedNext(int checkMode); 57 } 58 59 private static class EmptyCallback implements Callback { 60 public static final Callback INSTANCE = new EmptyCallback(); 61 @Override public void onEnableProceedButtons(boolean enable) { } 62 @Override public void onProceedNext(int checkMode) { } 63 } 64 65 /** 66 * Called when a fragment is first attached to its activity. 67 * {@link #onCreate(Bundle)} will be called after this. 68 */ 69 @Override 70 public void onAttach(Activity activity) { 71 super.onAttach(activity); 72 mContext = activity; 73 74 // Notify the activity that we're here. 75 if (activity instanceof AccountSettingsXL) { 76 ((AccountSettingsXL)activity).onAttach(this); 77 } 78 } 79 80 /** 81 * Called to do initial creation of a fragment. This is called after 82 * {@link #onAttach(Activity)} and before {@link #onActivityCreated(Bundle)}. 83 */ 84 @Override 85 public void onCreate(Bundle savedInstanceState) { 86 super.onCreate(savedInstanceState); 87 88 setHasOptionsMenu(true); 89 mNextButtonEnabled = false; 90 } 91 92 // Add a "Next" button when this fragment is displayed 93 @Override 94 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 95 super.onCreateOptionsMenu(menu, inflater); 96 inflater.inflate(R.menu.account_settings_next_option, menu); 97 } 98 99 /** 100 * Enable/disable "next" button 101 */ 102 @Override 103 public void onPrepareOptionsMenu(Menu menu) { 104 MenuItem item = menu.findItem(R.id.next); 105 item.setEnabled(mNextButtonEnabled); 106 } 107 108 /** 109 * Respond to clicks in the "Next" button 110 */ 111 @Override 112 public boolean onOptionsItemSelected(MenuItem item) { 113 switch (item.getItemId()) { 114 case R.id.next: 115 onNext(); 116 return true; 117 } 118 return super.onOptionsItemSelected(item); 119 } 120 121 /** 122 * Activity provides callbacks here. 123 */ 124 public void setCallback(Callback callback) { 125 mCallback = (callback == null) ? EmptyCallback.INSTANCE : callback; 126 mContext = getActivity(); 127 } 128 129 /** 130 * Enable/disable the "next" button 131 */ 132 public void enableNextButton(boolean enable) { 133 // We have to set mNextButtonEnabled first, because invalidateOptionsMenu calls 134 // onPrepareOptionsMenu immediately 135 boolean wasEnabled = mNextButtonEnabled; 136 mNextButtonEnabled = enable; 137 138 if (enable != wasEnabled) { 139 getActivity().invalidateOptionsMenu(); 140 } 141 142 // TODO: This supports the legacy activities and will be removed 143 mCallback.onEnableProceedButtons(enable); 144 } 145 146 /** 147 * Handle OK result from check settings. Save settings, and exit to previous fragment. 148 */ 149 public void onCheckSettingsOk() { 150 saveSettingsAfterEdit(); 151 getActivity().onBackPressed(); 152 } 153 154 /** 155 * Save settings after "OK" result from checker. Concrete classes must implement. 156 */ 157 public abstract void saveSettingsAfterEdit(); 158 159 /** 160 * Respond to a click of the "Next" button. Concrete classes must implement. 161 */ 162 public abstract void onNext(); 163} 164