blob: e49e80df7da990369b93325e22dc0f6e5f55d711 [file] [log] [blame]
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.systemui.keyguard;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Icon;
import android.icu.text.DateFormat;
import android.icu.text.DisplayContext;
import android.net.Uri;
import android.os.Handler;
import android.text.TextUtils;
import com.android.internal.annotations.VisibleForTesting;
import com.android.systemui.R;
import com.android.systemui.statusbar.policy.NextAlarmController;
import com.android.systemui.statusbar.policy.NextAlarmControllerImpl;
import java.util.Date;
import java.util.Locale;
import androidx.app.slice.Slice;
import androidx.app.slice.SliceProvider;
import androidx.app.slice.builders.ListBuilder;
import androidx.app.slice.builders.ListBuilder.RowBuilder;
/**
* Simple Slice provider that shows the current date.
*/
public class KeyguardSliceProvider extends SliceProvider implements
NextAlarmController.NextAlarmChangeCallback {
public static final String KEYGUARD_SLICE_URI = "content://com.android.systemui.keyguard/main";
public static final String KEYGUARD_DATE_URI = "content://com.android.systemui.keyguard/date";
public static final String KEYGUARD_NEXT_ALARM_URI =
"content://com.android.systemui.keyguard/alarm";
private final Date mCurrentTime = new Date();
protected final Uri mSliceUri;
protected final Uri mDateUri;
protected final Uri mAlarmUri;
private final Handler mHandler;
private String mDatePattern;
private DateFormat mDateFormat;
private String mLastText;
private boolean mRegistered;
private boolean mRegisteredEveryMinute;
private String mNextAlarm;
private NextAlarmController mNextAlarmController;
/**
* 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);
}
}
};
public KeyguardSliceProvider() {
this(new Handler());
}
@VisibleForTesting
KeyguardSliceProvider(Handler handler) {
mHandler = handler;
mSliceUri = Uri.parse(KEYGUARD_SLICE_URI);
mDateUri = Uri.parse(KEYGUARD_DATE_URI);
mAlarmUri = Uri.parse(KEYGUARD_NEXT_ALARM_URI);
}
@Override
public Slice onBindSlice(Uri sliceUri) {
ListBuilder builder = new ListBuilder(getContext(), mSliceUri);
builder.addRow(new RowBuilder(builder, mDateUri).setTitle(mLastText));
if (!TextUtils.isEmpty(mNextAlarm)) {
Icon icon = Icon.createWithResource(getContext(), R.drawable.ic_access_alarms_big);
builder.addRow(new RowBuilder(builder, mAlarmUri)
.setTitle(mNextAlarm).addEndItem(icon));
}
return builder.build();
}
@Override
public boolean onCreateSliceProvider() {
mNextAlarmController = new NextAlarmControllerImpl(getContext());
mNextAlarmController.addCallback(this);
mDatePattern = getContext().getString(R.string.system_ui_date_pattern);
registerClockUpdate(false /* everyMinute */);
updateClock();
return true;
}
public static String formatNextAlarm(Context context, AlarmManager.AlarmClockInfo info) {
if (info == null) {
return "";
}
String skeleton = android.text.format.DateFormat
.is24HourFormat(context, ActivityManager.getCurrentUser()) ? "EHm" : "Ehma";
String pattern = android.text.format.DateFormat
.getBestDateTimePattern(Locale.getDefault(), skeleton);
return android.text.format.DateFormat.format(pattern, info.getTriggerTime()).toString();
}
/**
* Registers a broadcast receiver for clock updates, include date, time zone and manually
* changing the date/time via the settings app.
*
* @param everyMinute {@code true} if you also want updates every minute.
*/
protected void registerClockUpdate(boolean everyMinute) {
if (mRegistered) {
if (mRegisteredEveryMinute == everyMinute) {
return;
} else {
unregisterClockUpdate();
}
}
IntentFilter filter = new IntentFilter();
if (everyMinute) {
filter.addAction(Intent.ACTION_TIME_TICK);
}
filter.addAction(Intent.ACTION_DATE_CHANGED);
filter.addAction(Intent.ACTION_TIME_CHANGED);
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
filter.addAction(Intent.ACTION_LOCALE_CHANGED);
getContext().registerReceiver(mIntentReceiver, filter, null /* permission*/,
null /* scheduler */);
mRegistered = true;
mRegisteredEveryMinute = everyMinute;
}
protected void unregisterClockUpdate() {
if (!mRegistered) {
return;
}
getContext().unregisterReceiver(mIntentReceiver);
mRegistered = false;
}
@VisibleForTesting
boolean isRegistered() {
return mRegistered;
}
protected void updateClock() {
final String text = getFormattedDate();
if (!text.equals(mLastText)) {
mLastText = text;
getContext().getContentResolver().notifyChange(mSliceUri, null /* observer */);
}
}
protected String getFormattedDate() {
if (mDateFormat == null) {
final Locale l = Locale.getDefault();
DateFormat format = DateFormat.getInstanceForSkeleton(mDatePattern, l);
format.setContext(DisplayContext.CAPITALIZATION_FOR_STANDALONE);
mDateFormat = format;
}
mCurrentTime.setTime(System.currentTimeMillis());
return mDateFormat.format(mCurrentTime);
}
@VisibleForTesting
void cleanDateFormat() {
mDateFormat = null;
}
@Override
public void onNextAlarmChanged(AlarmManager.AlarmClockInfo nextAlarm) {
mNextAlarm = formatNextAlarm(getContext(), nextAlarm);
getContext().getContentResolver().notifyChange(mSliceUri, null /* observer */);
}
}