Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c999316f authored by Isaac Katzenelson's avatar Isaac Katzenelson
Browse files

Fix today icon in search activity

Bug: 6465067 Today icon missing date when searching in landscape mode
Change-Id: I7ea2654c67edc2cfd1dc21d6d97705320b593e94
parent 84fbd42c
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@
        android:id="@+id/action_today"
        android:alphabeticShortcut="t"
        android:title="@string/goto_today"
        android:icon="@drawable/ic_menu_today_holo_light"
        android:icon="@drawable/today_icon"
        android:showAsAction="ifRoom" />
    <item
        android:id="@+id/action_settings"
+12 −50
Original line number Diff line number Diff line
@@ -162,7 +162,6 @@ public class AllInOneActivity extends Activity implements EventHandler,
    private String mShowString;

    DayOfMonthDrawable mDayOfMonthIcon;
    Handler mMidnightUpdaterHandler;

    int mOrientation;

@@ -241,16 +240,20 @@ public class AllInOneActivity extends Activity implements EventHandler,
    private final Runnable mHomeTimeUpdater = new Runnable() {
        @Override
        public void run() {
            mTimeZone = Utils.getTimeZone(AllInOneActivity.this, mHomeTimeUpdater);
            updateSecondaryTitleFields(-1);
            AllInOneActivity.this.invalidateOptionsMenu();
            Utils.setMidnightUpdater(mHandler, mTimeChangesUpdater, mTimeZone);
        }
    };

    // runs every midnight and refreshes the today icon
    private final Runnable mMidnightUpdater = new Runnable() {
    // runs every midnight/time changes and refreshes the today icon
    private final Runnable mTimeChangesUpdater = new Runnable() {
        @Override
        public void run() {
            mTimeZone = Utils.getTimeZone(AllInOneActivity.this, mHomeTimeUpdater);
            AllInOneActivity.this.invalidateOptionsMenu();
            Utils.setMidnightUpdater(mHandler, mTimeChangesUpdater, mTimeZone);
        }
    };

@@ -269,23 +272,7 @@ public class AllInOneActivity extends Activity implements EventHandler,
        }
    };

    private class CalendarBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_DATE_CHANGED) ||
                    intent.getAction().equals(Intent.ACTION_TIME_CHANGED) ||
                    intent.getAction().equals(Intent.ACTION_LOCALE_CHANGED) ||
                    intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)) {
                // Update the day of month in the today button by invalidating
                // the option menu thus forcing it to redraw and update the day of month
                Utils.setMidnightUpdater(mHandler, mMidnightUpdater, mTimeZone);
                AllInOneActivity.this.invalidateOptionsMenu();
            }
        }
    }

    CalendarBroadcastReceiver mCalIntentReceiver;
    BroadcastReceiver mCalIntentReceiver;

    @Override
    protected void onNewIntent(Intent intent) {
@@ -578,23 +565,11 @@ public class AllInOneActivity extends Activity implements EventHandler,
            mIntentEventEndMillis = -1;
            mIntentAllDay = false;
        }
        if (mMidnightUpdaterHandler == null) {
            mMidnightUpdaterHandler = new Handler();
        }
        Utils.setMidnightUpdater(mHandler, mMidnightUpdater, mTimeZone);
        Utils.setMidnightUpdater(mHandler, mTimeChangesUpdater, mTimeZone);
        // Make sure the today icon is up to date
        invalidateOptionsMenu();

              // Register for Intent broadcasts
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_CHANGED);
        filter.addAction(Intent.ACTION_DATE_CHANGED);
        filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        filter.addAction(Intent.ACTION_LOCALE_CHANGED);
        if (mCalIntentReceiver == null) {
            mCalIntentReceiver = new CalendarBroadcastReceiver();
        }
        registerReceiver(mCalIntentReceiver, filter);
        mCalIntentReceiver = Utils.setTimeChangesReceiver(this, mTimeChangesUpdater);
    }

    @Override
@@ -617,8 +592,8 @@ public class AllInOneActivity extends Activity implements EventHandler,
        if (mController.getViewType() != ViewType.EDIT) {
            Utils.setDefaultView(this, mController.getViewType());
        }
        Utils.resetMidnightUpdater(mHandler, mMidnightUpdater);
        unregisterReceiver(mCalIntentReceiver);
        Utils.resetMidnightUpdater(mHandler, mTimeChangesUpdater);
        Utils.clearTimeChangesReceiver(this, mCalIntentReceiver);
    }

    @Override
@@ -768,9 +743,7 @@ public class AllInOneActivity extends Activity implements EventHandler,
        // replace the default top layer drawable of the today icon with a custom drawable
        // that shows the day of the month of today
        LayerDrawable icon = (LayerDrawable)menu.findItem(R.id.action_today).getIcon();
        setTodayIcon();
        icon.mutate();
        icon.setDrawableByLayerId(R.id.today_icon_day, mDayOfMonthIcon);
        Utils.setTodayIcon(icon, this, mTimeZone);
        return true;
    }

@@ -1343,15 +1316,4 @@ public class AllInOneActivity extends Activity implements EventHandler,
        }
        return false;
    }

    // Updates the day of the month drawable in the today icon
    private void setTodayIcon() {
        if (mDayOfMonthIcon == null) {
            mDayOfMonthIcon = new DayOfMonthDrawable(this);
        }
        Time now = new Time();
        now.set(System.currentTimeMillis());
        now.normalize(false);
        mDayOfMonthIcon.setDayOfMonth(now.monthDay);
    }
}
+30 −0
Original line number Diff line number Diff line
@@ -23,10 +23,12 @@ import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.SearchManager;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Intent;
import android.database.ContentObserver;
import android.graphics.drawable.LayerDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
@@ -74,6 +76,8 @@ public class SearchActivity extends Activity implements CalendarController.Event

    private DeleteEventHelper mDeleteEventHelper;

    private Handler mHandler;
    private BroadcastReceiver mTimeChangesReceiver;
    private ContentResolver mContentResolver;

    private final ContentObserver mObserver = new ContentObserver(new Handler()) {
@@ -88,11 +92,22 @@ public class SearchActivity extends Activity implements CalendarController.Event
        }
    };

    // runs when a timezone was changed and updates the today icon
    private final Runnable mTimeChangesUpdater = new Runnable() {
        @Override
        public void run() {
            Utils.setMidnightUpdater(mHandler, mTimeChangesUpdater,
                    Utils.getTimeZone(SearchActivity.this, mTimeChangesUpdater));
            SearchActivity.this.invalidateOptionsMenu();
        }
    };

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // This needs to be created before setContentView
        mController = CalendarController.getInstance(this);
        mHandler = new Handler();

        mIsMultipane = Utils.getConfigBool(this, R.bool.multiple_pane_config);
        mShowEventDetailsWithAgenda =
@@ -232,6 +247,12 @@ public class SearchActivity extends Activity implements CalendarController.Event
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.search_title_bar, menu);

        // replace the default top layer drawable of the today icon with a custom drawable
        // that shows the day of the month of today
        LayerDrawable icon = (LayerDrawable)menu.findItem(R.id.action_today).getIcon();
        Utils.setTodayIcon(icon, this, Utils.getTimeZone(SearchActivity.this, mTimeChangesUpdater));

        MenuItem item = menu.findItem(R.id.action_search);
        item.expandActionView();
        item.setOnActionExpandListener(this);
@@ -239,6 +260,7 @@ public class SearchActivity extends Activity implements CalendarController.Event
        Utils.setUpSearchView(mSearchView, this);
        mSearchView.setQuery(mQuery, false);
        mSearchView.clearFocus();

        return true;
    }

@@ -293,6 +315,12 @@ public class SearchActivity extends Activity implements CalendarController.Event
    @Override
    protected void onResume() {
        super.onResume();

        Utils.setMidnightUpdater(
                mHandler, mTimeChangesUpdater, Utils.getTimeZone(this, mTimeChangesUpdater));
        // Make sure the today icon is up to date
        invalidateOptionsMenu();
        mTimeChangesReceiver = Utils.setTimeChangesReceiver(this, mTimeChangesUpdater);
        mContentResolver.registerContentObserver(Events.CONTENT_URI, true, mObserver);
        // We call this in case the user changed the time zone
        eventsChanged();
@@ -301,6 +329,8 @@ public class SearchActivity extends Activity implements CalendarController.Event
    @Override
    protected void onPause() {
        super.onPause();
        Utils.resetMidnightUpdater(mHandler, mTimeChangesUpdater);
        Utils.clearTimeChangesReceiver(this, mTimeChangesReceiver);
        mContentResolver.unregisterContentObserver(mObserver);
    }

+65 −2
Original line number Diff line number Diff line
@@ -18,17 +18,19 @@ package com.android.calendar;

import static android.provider.CalendarContract.EXTRA_EVENT_BEGIN_TIME;

import com.android.calendar.CalendarController.ViewType;

import android.app.Activity;
import android.app.SearchManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
@@ -39,6 +41,7 @@ import android.text.format.Time;
import android.util.Log;
import android.widget.SearchView;

import com.android.calendar.CalendarController.ViewType;
import com.android.calendar.CalendarUtils.TimeZoneUtils;

import java.util.ArrayList;
@@ -1367,4 +1370,64 @@ public class Utils {
    public static boolean isValidEmail(String email) {
        return email != null && !email.endsWith(MACHINE_GENERATED_ADDRESS);
    }

    /**
     * Inserts a drawable with today's day into the today's icon in the option menu
     * @param icon - today's icon from the options menu
     */
    public static void setTodayIcon(LayerDrawable icon, Context c, String timezone) {
        DayOfMonthDrawable today;

        // Reuse current drawable if possible
        Drawable currentDrawable = icon.findDrawableByLayerId(R.id.today_icon_day);
        if (currentDrawable != null && currentDrawable instanceof DayOfMonthDrawable) {
            today = (DayOfMonthDrawable)currentDrawable;
        } else {
            today = new DayOfMonthDrawable(c);
        }
        // Set the day and update the icon
        Time now =  new Time(timezone);
        now.setToNow();
        now.normalize(false);
        today.setDayOfMonth(now.monthDay);
        icon.mutate();
        icon.setDrawableByLayerId(R.id.today_icon_day, today);
    }

    private static class CalendarBroadcastReceiver extends BroadcastReceiver {

        Runnable mCallBack;

        public CalendarBroadcastReceiver(Runnable callback) {
            super();
            mCallBack = callback;
        }
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_DATE_CHANGED) ||
                    intent.getAction().equals(Intent.ACTION_TIME_CHANGED) ||
                    intent.getAction().equals(Intent.ACTION_LOCALE_CHANGED) ||
                    intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)) {
                if (mCallBack != null) {
                    mCallBack.run();
                }
            }
        }
    }

    public static BroadcastReceiver setTimeChangesReceiver(Context c, Runnable callback) {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_CHANGED);
        filter.addAction(Intent.ACTION_DATE_CHANGED);
        filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        filter.addAction(Intent.ACTION_LOCALE_CHANGED);

        CalendarBroadcastReceiver r = new CalendarBroadcastReceiver(callback);
        c.registerReceiver(r, filter);
        return r;
    }

    public static void clearTimeChangesReceiver(Context c, BroadcastReceiver r) {
        c.unregisterReceiver(r);
    }
}