《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);
    }
    

你可能感兴趣的:(应用)