FileDocCategorySizeDatePackage
SplitClockView.javaAPI DocAndroid 5.1 API5081Thu Mar 12 22:22:42 GMT 2015com.android.systemui.statusbar.policy

SplitClockView

public class SplitClockView extends android.widget.LinearLayout
Container for a clock which has two separate views for the clock itself and AM/PM indicator. This is used to scale the clock independently of AM/PM.

Fields Summary
private android.widget.TextClock
mTimeView
private android.widget.TextClock
mAmPmView
private android.content.BroadcastReceiver
mIntentReceiver
Constructors Summary
public SplitClockView(android.content.Context context, android.util.AttributeSet attrs)


         
        super(context, attrs);
    
Methods Summary
private static intgetAmPmPartEndIndex(java.lang.String formatString)

return
the index where the AM/PM part starts at the end in {@code formatString} including leading white spaces or {@code -1} if no AM/PM part is found or {@code formatString} doesn't end with AM/PM part

        boolean hasAmPm = false;
        int length = formatString.length();
        for (int i = length - 1; i >= 0; i--) {
            char c = formatString.charAt(i);
            boolean isAmPm = c == 'a";
            boolean isWhitespace = Character.isWhitespace(c);
            if (isAmPm) {
                hasAmPm = true;
            }
            if (isAmPm || isWhitespace) {
                continue;
            }
            if (i == length - 1) {

                // First character was not AM/PM and not whitespace, so it's not ending with AM/PM.
                return -1;
            } else {

                // If we have AM/PM at all, return last index, or -1 to indicate that it's not
                // ending with AM/PM.
                return hasAmPm ? i + 1 : -1;
            }
        }

        // Only AM/PM and whitespaces? The whole string is AM/PM. Else: Only whitespaces in the
        // string.
        return hasAmPm ? 0 : -1;
    
protected voidonAttachedToWindow()

        super.onAttachedToWindow();

        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_CHANGED);
        filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        filter.addAction(Intent.ACTION_LOCALE_CHANGED);
        filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
        filter.addAction(Intent.ACTION_USER_SWITCHED);
        getContext().registerReceiverAsUser(mIntentReceiver, UserHandle.ALL, filter, null, null);

        updatePatterns();
    
protected voidonDetachedFromWindow()

        super.onDetachedFromWindow();
        getContext().unregisterReceiver(mIntentReceiver);
    
protected voidonFinishInflate()

        super.onFinishInflate();
        mTimeView = (TextClock) findViewById(R.id.time_view);
        mAmPmView = (TextClock) findViewById(R.id.am_pm_view);
        mTimeView.setShowCurrentUserTime(true);
        mAmPmView.setShowCurrentUserTime(true);
    
private voidupdatePatterns()

        String formatString = DateFormat.getTimeFormatString(getContext(),
                ActivityManager.getCurrentUser());
        int index = getAmPmPartEndIndex(formatString);
        String timeString;
        String amPmString;
        if (index == -1) {
            timeString = formatString;
            amPmString = "";
        } else {
            timeString = formatString.substring(0, index);
            amPmString = formatString.substring(index);
        }
        mTimeView.setFormat12Hour(timeString);
        mTimeView.setFormat24Hour(timeString);
        mAmPmView.setFormat12Hour(amPmString);
        mAmPmView.setFormat24Hour(amPmString);