content: /*
   * Copyright (C) 2014 AChep@xda <artemchep@gmail.com>
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * as published by the Free Software Foundation; either version 2
   * of the License, or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   * MA  02110-1301, USA.
   */
  package com.achep.acdisplay.ui.widgets;
  
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.content.IntentFilter;
  import android.os.Build;
  import android.text.format.Time;
  import android.util.AttributeSet;
  
  import com.achep.base.ui.widgets.TextView;
  import com.achep.base.utils.DateUtils;
  
  import java.util.GregorianCalendar;
  
  /**
   * Created by Artem on 29.01.14.
   */
  public class TimeView extends TextView {
  
      private static final String TAG = "TimeView";
  
      private int mLastTime = -1;
  
      private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
              switch (intent.getAction()) {
                  case Intent.ACTION_TIME_TICK:
                  case Intent.ACTION_TIME_CHANGED:
                  case Intent.ACTION_TIMEZONE_CHANGED:
                      updateClock();
                      break;
              }
          }
      };
  
      public TimeView(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
  
      @Override
      protected void onAttachedToWindow() {
          super.onAttachedToWindow();
  
          IntentFilter filter = new IntentFilter();
          filter.addAction(Intent.ACTION_TIME_TICK);
          filter.addAction(Intent.ACTION_TIME_CHANGED);
          filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
          getContext().registerReceiver(mIntentReceiver, filter, null, null);
  
          updateClock();
      }
  
      @Override
      protected void onDetachedFromWindow() {
          super.onDetachedFromWindow();
          getContext().unregisterReceiver(mIntentReceiver);
      }
  
      protected void updateClock() {
  
          if(Build.VERSION.SDK_INT< Build.VERSION_CODES.LOLLIPOP_MR1)
          {
              Time time = new Time();
              time.setToNow();
              int now = time.hour * 60 + time.minute;
              if (now != mLastTime) {
                  setText(DateUtils.formatTime(getContext(), time.hour, time.minute));
                  mLastTime = now;
              }
          }
          else {
              GregorianCalendar gregorianCalendar = new GregorianCalendar();
              int now = gregorianCalendar.get(GregorianCalendar.HOUR) * 60 + gregorianCalendar.get(GregorianCalendar.MINUTE);
              if (now != mLastTime) {
                  setText(DateUtils.formatTime(getContext(), gregorianCalendar.get(GregorianCalendar.HOUR), gregorianCalendar.get(GregorianCalendar.MINUTE)));
                  mLastTime = now;
              }
          }
      }
  }
  