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