package com.malaspina.dashclocktimezone;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.google.android.apps.dashclock.api.DashClockExtension;
import com.google.android.apps.dashclock.api.ExtensionData;

import net.danlew.android.joda.DateUtils;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

/**
 * Created by Ryan on 2/24/2016.
 */
public class ExtensionBase extends DashClockExtension {

    private String timezone_key;
    private String hour_format_key;
    private String ext_format_key;

    SharedPreferences prefs;

    private final static IntentFilter INTENT_FILTER;

    static {
        INTENT_FILTER = new IntentFilter();
        INTENT_FILTER.addAction(Intent.ACTION_TIME_TICK);
        INTENT_FILTER.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        INTENT_FILTER.addAction(Intent.ACTION_TIME_CHANGED);
    }

    @Override
    protected void onInitialize(boolean isReconnect) {
        setUpdateWhenScreenOn(true);
        super.onInitialize(isReconnect);
        registerReceiver(mTimeInfoReceiver, INTENT_FILTER);
    }

    @Override
    protected void onUpdateData(int reason) {

        prefs = PreferenceManager.getDefaultSharedPreferences(this);

        String timezone = prefs.getString(getTimezone_key(),
                getString(R.string.default_timezone));
        String hourFormat = prefs.getString(getHour_format_key(),
                getString(R.string.default_hour_format));
        String extFormat  = prefs.getString(getExt_format_key(),
                getString(R.string.default_extended_format));

        ExtensionData data = new ExtensionData();

        data.visible(true);
        data.icon(R.mipmap.ic_stat);
        data.status(getTime(timezone));
        data.expandedTitle(getTime(timezone));
        data.expandedBody(timezone);

        publishUpdate(data);
    }

    private String getTime(String timezone) {
        DateTime now = new DateTime();
        DateTimeZone tz = DateTimeZone.forID(timezone);
        DateTime now_tz = now.toDateTime(tz);
        return DateUtils.formatDateTime(this, now_tz, DateUtils.FORMAT_SHOW_TIME);
    }

    // If time changed, update extension
    private BroadcastReceiver mTimeInfoReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent intent) {
            final String action = intent.getAction();
            if (action.equals(Intent.ACTION_TIME_TICK) ||
                    action.equals(Intent.ACTION_TIME_CHANGED) ||
                    action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
                onUpdateData(0);
            }
        }
    };

    @Override
    public void onDestroy() {
        unregisterReceiver(mTimeInfoReceiver);
        super.onDestroy();
    }

    public String getTimezone_key() {
        return timezone_key;
    }

    public void setTimezone_key(String timezone_key) {
        this.timezone_key = timezone_key;
    }

    public String getHour_format_key() {
        return hour_format_key;
    }

    public void setHour_format_key(String hour_format_key) {
        this.hour_format_key = hour_format_key;
    }

    public String getExt_format_key() {
        return ext_format_key;
    }

    public void setExt_format_key(String ext_format_key) {
        this.ext_format_key = ext_format_key;
    }
}

@@ -499,16 +499,107 @@
 imeZone;
+%0Aimport org.joda.time.format.DateTimeFormat;%0Aimport org.joda.time.format.DateTimeFormatter;
 %0A%0A/**%0A *
@@ -629,16 +629,19 @@
 4/2016.%0A
+ *%0A
  */%0Apubl
@@ -1987,32 +1987,44 @@
 getTime(timezone
+, hourFormat
 ));%0A        data
@@ -2046,32 +2046,44 @@
 getTime(timezone
+, hourFormat
 ));%0A        data
@@ -2186,16 +2186,35 @@
 timezone
+, String hourFormat
 ) %7B%0A    
@@ -2362,39 +2362,43 @@
     
-return DateUtils.
+DateTimeFormatter 
 format
+ = 
 DateTime
 (thi
@@ -2397,49 +2397,73 @@
 Time
-(this, now_tz, DateUtils.FORMAT_SHOW_TIME
+Format.forPattern(hourFormat);%0A        return format.print(now_tz
 );%0A 

Adjust to hour format