package com.android.systemui.statusbar.phone; import com.android.systemui.FontSizeUtils; import android.content.ContentResolver; import android.content.Context; import android.database.ContentObserver; import android.graphics.Color; import android.os.Handler; import android.os.UserHandle; import android.view.View; import com.android.systemui.R; import com.android.systemui.cm.UserContentObserver; import com.android.systemui.statusbar.policy.Clock; import cyanogenmod.providers.CMSettings; /** * To control your...clock */ public class ClockController { public static final int STYLE_HIDE_CLOCK = 0; public static final int STYLE_CLOCK_RIGHT = 1; public static final int STYLE_CLOCK_CENTER = 2; public static final int STYLE_CLOCK_LEFT = 3; private final IconMerger mNotificationIcons; private final Context mContext; private final SettingsObserver mSettingsObserver; private Clock mRightClock, mCenterClock, mLeftClock, mActiveClock; private int mClockLocation; private int mAmPmStyle; private int mIconTint = Color.WHITE; class SettingsObserver extends UserContentObserver { SettingsObserver(Handler handler) { super(handler); } @Override protected void observe() { super.observe(); ContentResolver resolver = mContext.getContentResolver(); resolver.registerContentObserver(CMSettings.System.getUriFor( CMSettings.System.STATUS_BAR_AM_PM), false, this, UserHandle.USER_ALL); resolver.registerContentObserver(CMSettings.System.getUriFor( CMSettings.System.STATUS_BAR_CLOCK), false, this, UserHandle.USER_ALL); updateSettings(); } @Override protected void unobserve() { super.unobserve(); mContext.getContentResolver().unregisterContentObserver(this); } @Override public void update() { updateSettings(); } } public ClockController(View statusBar, IconMerger notificationIcons, Handler handler) { mRightClock = (Clock) statusBar.findViewById(R.id.clock); mCenterClock = (Clock) statusBar.findViewById(R.id.center_clock); mLeftClock = (Clock) statusBar.findViewById(R.id.left_clock); mNotificationIcons = notificationIcons; mContext = statusBar.getContext(); mActiveClock = mRightClock; mSettingsObserver = new SettingsObserver(handler); mSettingsObserver.observe(); } private Clock getClockForCurrentLocation() { Clock clockForAlignment; switch (mClockLocation) { case STYLE_CLOCK_CENTER: clockForAlignment = mCenterClock; break; case STYLE_CLOCK_LEFT: clockForAlignment = mLeftClock; break; case STYLE_CLOCK_RIGHT: case STYLE_HIDE_CLOCK: default: clockForAlignment = mRightClock; break; } return clockForAlignment; } private void updateActiveClock() { mActiveClock.setVisibility(View.GONE); if (mClockLocation == STYLE_HIDE_CLOCK) { return; } mActiveClock = getClockForCurrentLocation(); mActiveClock.setVisibility(View.VISIBLE); mActiveClock.setAmPmStyle(mAmPmStyle); setClockAndDateStatus(); setTextColor(mIconTint); updateFontSize(); } private void updateSettings() { ContentResolver resolver = mContext.getContentResolver(); mAmPmStyle = CMSettings.System.getIntForUser(resolver, CMSettings.System.STATUS_BAR_AM_PM, Clock.AM_PM_STYLE_GONE, UserHandle.USER_CURRENT); mClockLocation = CMSettings.System.getIntForUser( resolver, CMSettings.System.STATUS_BAR_CLOCK, STYLE_CLOCK_RIGHT, UserHandle.USER_CURRENT); updateActiveClock(); } private void setClockAndDateStatus() { if (mNotificationIcons != null) { mNotificationIcons.setClockAndDateStatus(mClockLocation); } } public void setVisibility(boolean visible) { if (mActiveClock != null) { mActiveClock.setVisibility(visible ? View.VISIBLE : View.GONE); } } public void setTextColor(int iconTint) { mIconTint = iconTint; if (mActiveClock != null) { mActiveClock.setTextColor(iconTint); } } public void updateFontSize() { if (mActiveClock != null) { FontSizeUtils.updateFontSize(mActiveClock, R.dimen.status_bar_clock_size); } } public void cleanup() { mSettingsObserver.unobserve(); } }