1. 程式人生 > >《SystemUI》修改SystemUI鎖屏介面時間格式

《SystemUI》修改SystemUI鎖屏介面時間格式

要求:修改SystemUI鎖屏介面時間格式

Android P 上Sysyemui鎖屏介面上的日期顯示不在DateView處理了,使用KeyguardSliceProvider來處理,繼承Contentprovide

之前Android O修改日期直接是在
SystemUI/src/com/android/systemui/statusbar/policy/DateView.java

    public DateView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.DateView,
                0, 0);

        try {
            mDatePattern = a.getString(R.styleable.DateView_datePattern);
        } finally {
            a.recycle();
        }
        if (mDatePattern == null) {
            mDatePattern = getContext().getString(R.string.system_ui_date_pattern);
        }
+
+        String date_format = android.provider.Settings.System.getString(
+                getContext().getContentResolver(),
+                android.provider.Settings.System.DATE_FORMAT);
+        
+        if(!TextUtils.isEmpty(date_format)){
+            mDatePattern = date_format;
+        }
+
    }

Android P修改在SystemUI/src/com/android/systemui/keyguard/KeyguardSliceProvider.java

     public boolean onCreateSliceProvider() {
         mAlarmManager = getContext().getSystemService(AlarmManager.class);
         mContentResolver = getContext().getContentResolver();
         mNextAlarmController = new NextAlarmControllerImpl(getContext());
         mNextAlarmController.addCallback(this);
         mZenModeController = new ZenModeControllerImpl(getContext(), mHandler);
         mZenModeController.addCallback(this);
         mDatePattern = getContext().getString(R.string.system_ui_aod_date_pattern);
+
+        String date_format = android.provider.Settings.System.getString(
+                getContext().getContentResolver(),
+                android.provider.Settings.System.DATE_FORMAT);
+        
+        if(!TextUtils.isEmpty(date_format)){
+            mDatePattern = date_format;
+        }
+
         registerClockUpdate();
         updateClock();
         return true;
     }

分析Anndroid P上處理方式

接受到時間變化,或這個上面初始的時候,呼叫updateClock方法
    /**
     * Receiver responsible for time ticking and updating the date format.
     */
    @VisibleForTesting
    final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (Intent.ACTION_TIME_TICK.equals(action)
                    || Intent.ACTION_DATE_CHANGED.equals(action)
                    || Intent.ACTION_TIME_CHANGED.equals(action)
                    || Intent.ACTION_TIMEZONE_CHANGED.equals(action)
                    || Intent.ACTION_LOCALE_CHANGED.equals(action)) {
                if (Intent.ACTION_LOCALE_CHANGED.equals(action)
                        || Intent.ACTION_TIMEZONE_CHANGED.equals(action)) {
                    // need to get a fresh date format
                    mHandler.post(KeyguardSliceProvider.this::cleanDateFormat);
                }
                mHandler.post(KeyguardSliceProvider.this::updateClock);
            }
        }
    };
    

獲取最新的時間顯示,然後通知contentprovide更新顯示。mLastText的值與Slice關聯,現實的時候就跟新了。
    protected void updateClock() {
        final String text = getFormattedDate();
        if (!text.equals(mLastText)) {
            mLastText = text;
            mContentResolver.notifyChange(mSliceUri, null /* observer */);
        }
    }

    protected String getFormattedDate() {
        if (mDateFormat == null) {
            final Locale l = Locale.getDefault();
            DateFormat format;
            String country = getContext().getResources().getConfiguration().locale.getCountry();
            if (country.equals("XA")) {
                format = DateFormat.getInstanceForSkeleton("eeeMMMd", l);
            }else{
                format = DateFormat.getInstanceForSkeleton(mDatePattern, l);
            }
            format.setContext(DisplayContext.CAPITALIZATION_FOR_STANDALONE);
            mDateFormat = format;
        }
        mCurrentTime.setTime(System.currentTimeMillis());
        return mDateFormat.format(mCurrentTime);
    }

    @Override
    public Slice onBindSlice(Uri sliceUri) {
        ListBuilder builder = new ListBuilder(getContext(), mSliceUri);
        builder.addRow(new RowBuilder(builder, mDateUri).setTitle(mLastText));
        addNextAlarm(builder);
        addZenMode(builder);
        addPrimaryAction(builder);
        return builder.build();
    }

    protected void addPrimaryAction(ListBuilder builder) {
        // Add simple action because API requires it; Keyguard handles presenting
        // its own slices so this action + icon are actually never used.
        PendingIntent pi = PendingIntent.getActivity(getContext(), 0, new Intent(), 0);
        Icon icon = Icon.createWithResource(getContext(), R.drawable.ic_access_alarms_big);
        SliceAction action = new SliceAction(pi, icon, mLastText);

        RowBuilder primaryActionRow = new RowBuilder(builder, Uri.parse(KEYGUARD_ACTION_URI))
            .setPrimaryAction(action);
        builder.addRow(primaryActionRow);
    }