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.AlarmManager; 20 import android.app.PendingIntent; 21 import android.content.BroadcastReceiver; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.IntentFilter; 25 import android.content.SharedPreferences; 26 import android.content.SharedPreferences.OnSharedPreferenceChangeListener; 27 import android.os.Bundle; 28 import android.os.Handler; 29 import android.preference.PreferenceManager; 30 import android.util.Log; 31 import android.view.LayoutInflater; 32 import android.view.MotionEvent; 33 import android.view.View; 34 import android.view.View.OnTouchListener; 35 import android.view.ViewConfiguration; 36 import android.view.ViewGroup; 37 import android.view.animation.AnimationUtils; 38 import android.widget.ListView; 39 40 import com.android.deskclock.worldclock.WorldClockAdapter; 41 42 /** 43 * Fragment that shows the clock (analog or digital), the next alarm info and the world clock. 44 */ 45 46 public class ClockFragment extends DeskClockFragment implements OnSharedPreferenceChangeListener { 47 48 private static final String BUTTONS_HIDDEN_KEY = "buttons_hidden"; 49 private final static String TAG = "ClockFragment"; 50 51 private View mButtons; 52 private boolean mButtonsHidden = false; 53 private View mDigitalClock, mAnalogClock, mClockFrame; 54 private WorldClockAdapter mAdapter; 55 private ListView mList; 56 private SharedPreferences mPrefs; 57 private String mDateFormat; 58 private String mDateFormatForAccessibility; 59 private String mDefaultClockStyle; 60 private String mClockStyle; 61 62 private PendingIntent mQuarterlyIntent; 63 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 64 @Override 65 public void onReceive(Context context, Intent intent) { 66 boolean changed = intent.getAction().equals(Intent.ACTION_TIME_CHANGED) 67 || intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED); 68 if (changed || intent.getAction().equals(Utils.ACTION_ON_QUARTER_HOUR)) { 69 Utils.updateDate(mDateFormat, mDateFormatForAccessibility,mClockFrame); 70 if (mAdapter != null) { 71 // *CHANGED may modify the need for showing the Home City 72 if (changed && (mAdapter.hasHomeCity() != mAdapter.needHomeCity())) { 73 mAdapter.loadData(context); 74 } else { 75 mAdapter.notifyDataSetChanged(); 76 } 77 } 78 } 79 if (changed || intent.getAction().equals(Alarms.ALARM_DONE_ACTION) 80 || intent.getAction().equals(Alarms.ALARM_SNOOZE_CANCELLED)) { 81 Utils.refreshAlarm(getActivity(), mClockFrame); 82 } 83 } 84 }; 85 86 private final Handler mHandler = new Handler(); 87 88 public ClockFragment() { 89 } 90 91 @Override 92 public View onCreateView(LayoutInflater inflater, ViewGroup container, 93 Bundle icicle) { 94 // Inflate the layout for this fragment 95 View v = inflater.inflate(R.layout.clock_fragment, container, false); 96 mButtons = v.findViewById(R.id.clock_buttons); 97 if (icicle != null) { 98 mButtonsHidden = icicle.getBoolean(BUTTONS_HIDDEN_KEY, false); 99 } 100 mList = (ListView)v.findViewById(R.id.cities); 101 mList.setDivider(null); 102 View headerView = inflater.inflate(R.layout.blank_header_view, mList, false); 103 mList.addHeaderView(headerView); 104 mClockFrame = inflater.inflate(R.layout.main_clock_frame, mList, false); 105 mDigitalClock = mClockFrame.findViewById(R.id.digital_clock); 106 mAnalogClock = mClockFrame.findViewById(R.id.analog_clock); 107 mList.addHeaderView(mClockFrame, null, false); 108 View footerView = inflater.inflate(R.layout.blank_footer_view, mList, false); 109 footerView.setBackgroundResource(R.color.blackish); 110 mList.addFooterView(footerView); 111 mAdapter = new WorldClockAdapter(getActivity()); 112 mList.setAdapter(mAdapter); 113 mList.setOnTouchListener(new OnTouchListener() { 114 private final float MAX_MOVEMENT_ALLOWED = 20; 115 private float mLastTouchX, mLastTouchY; 116 117 @Override 118 public boolean onTouch(View v, MotionEvent event) { 119 switch (event.getAction()) { 120 case (MotionEvent.ACTION_DOWN): 121 long time = Utils.getTimeNow(); 122 mHandler.postDelayed(new Runnable() { 123 @Override 124 public void run() { 125 startActivity(new Intent(getActivity(), ScreensaverActivity.class)); 126 } 127 }, ViewConfiguration.getLongPressTimeout()); 128 mLastTouchX = event.getX(); 129 mLastTouchY = event.getY(); 130 break; 131 case (MotionEvent.ACTION_MOVE): 132 float xDiff = Math.abs(event.getX()-mLastTouchX); 133 float yDiff = Math.abs(event.getY()-mLastTouchY); 134 if (xDiff >= MAX_MOVEMENT_ALLOWED || yDiff >= MAX_MOVEMENT_ALLOWED) { 135 mHandler.removeCallbacksAndMessages(null); 136 } 137 break; 138 default: 139 mHandler.removeCallbacksAndMessages(null); 140 } 141 return false; 142 } 143 }); 144 mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); 145 mDefaultClockStyle = getActivity().getResources().getString(R.string.default_clock_style); 146 return v; 147 } 148 149 @Override 150 public void onResume () { 151 super.onResume(); 152 mPrefs.registerOnSharedPreferenceChangeListener(this); 153 mDateFormat = getString(R.string.abbrev_wday_month_day_no_year); 154 mDateFormatForAccessibility = getString(R.string.full_wday_month_day_no_year); 155 156 long alarmOnQuarterHour = Utils.getAlarmOnQuarterHour(); 157 mQuarterlyIntent = PendingIntent.getBroadcast( 158 getActivity(), 0, new Intent(Utils.ACTION_ON_QUARTER_HOUR), 0); 159 ((AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE)).setRepeating( 160 AlarmManager.RTC, alarmOnQuarterHour, AlarmManager.INTERVAL_FIFTEEN_MINUTES, 161 mQuarterlyIntent); 162 // Besides monitoring when quarter-hour changes, monitor other actions that 163 // effect clock time 164 IntentFilter filter = new IntentFilter(Utils.ACTION_ON_QUARTER_HOUR); 165 filter.addAction(Alarms.ALARM_DONE_ACTION); 166 filter.addAction(Alarms.ALARM_SNOOZE_CANCELLED); 167 filter.addAction(Intent.ACTION_TIME_CHANGED); 168 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); 169 getActivity().registerReceiver(mIntentReceiver, filter); 170 171 mButtons.setAlpha(mButtonsHidden ? 0 : 1); 172 173 // Resume can invoked after changing the cities list. 174 if (mAdapter != null) { 175 mAdapter.reloadData(getActivity()); 176 } 177 // Resume can invoked after changing the clock style. 178 View clockView = Utils.setClockStyle(getActivity(), mDigitalClock, mAnalogClock, 179 SettingsActivity.KEY_CLOCK_STYLE); 180 mClockStyle = (clockView == mDigitalClock ? 181 Utils.CLOCK_TYPE_DIGITAL : Utils.CLOCK_TYPE_ANALOG); 182 mAdapter.notifyDataSetChanged(); 183 184 Utils.updateDate(mDateFormat, mDateFormatForAccessibility,mClockFrame); 185 Utils.refreshAlarm(getActivity(), mClockFrame); 186 } 187 188 @Override 189 public void onPause() { 190 super.onPause(); 191 mPrefs.unregisterOnSharedPreferenceChangeListener(this); 192 ((AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE)).cancel( 193 mQuarterlyIntent); 194 getActivity().unregisterReceiver(mIntentReceiver); 195 } 196 197 @Override 198 public void onSaveInstanceState (Bundle outState) { 199 outState.putBoolean(BUTTONS_HIDDEN_KEY, mButtonsHidden); 200 super.onSaveInstanceState(outState); 201 } 202 203 public void showButtons(boolean show) { 204 if (mButtons == null) { 205 return; 206 } 207 if (show && mButtonsHidden) { 208 mButtons.startAnimation( 209 AnimationUtils.loadAnimation(getActivity(), R.anim.unhide)); 210 mButtonsHidden = false; 211 } else if (!show && !mButtonsHidden) { 212 mButtons.startAnimation( 213 AnimationUtils.loadAnimation(getActivity(), R.anim.hide)); 214 mButtonsHidden = true; 215 } 216 } 217 218 @Override 219 public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { 220 if (key == SettingsActivity.KEY_CLOCK_STYLE) { 221 mClockStyle = prefs.getString(SettingsActivity.KEY_CLOCK_STYLE, mDefaultClockStyle); 222 mAdapter.notifyDataSetChanged(); 223 } 224 } 225 } 226