From 0b636d08439c291322339e02be605be54f589b91 Mon Sep 17 00:00:00 2001 From: DvTonder Date: Sun, 15 Jul 2012 10:28:15 -0400 Subject: Framework: Port CM9 features to CM10 This commit includes: - Power menu Reboot - Power menu screenshot - Profiles - Lock screen Calendar - Lock screen Weather - Notification light customization - Battery light customization - IME Selector notification toggle - and a few more things to support Settings Change-Id: Ibd63116df90b06f6ce6adb8a0343059bbb999bfb --- .../android/internal/widget/LockPatternUtils.java | 178 +++++++++++++++++++++ 1 file changed, 178 insertions(+) (limited to 'core/java/com/android/internal/widget/LockPatternUtils.java') diff --git a/core/java/com/android/internal/widget/LockPatternUtils.java b/core/java/com/android/internal/widget/LockPatternUtils.java index 75fef24..8f419e4 100644 --- a/core/java/com/android/internal/widget/LockPatternUtils.java +++ b/core/java/com/android/internal/widget/LockPatternUtils.java @@ -1,5 +1,6 @@ /* * Copyright (C) 2007 The Android Open Source Project + * Copyright (C) 2012 The CyanogenMod Project (Calendar) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +24,8 @@ import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; +import android.database.Cursor; +import android.net.Uri; import android.os.Binder; import android.os.Bundle; import android.os.IBinder; @@ -31,10 +34,13 @@ import android.os.ServiceManager; import android.os.SystemClock; import android.os.UserHandle; import android.os.storage.IMountService; +import android.provider.CalendarContract; import android.provider.Settings; import android.security.KeyStore; import android.telephony.TelephonyManager; import android.text.TextUtils; +import android.text.format.DateFormat; +import android.text.format.Time; import android.util.Log; import android.view.IWindowManager; import android.view.View; @@ -47,7 +53,10 @@ import com.google.android.collect.Lists; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.List; +import java.util.TimeZone; /** * Utilities for the lock pattern and its settings. @@ -1038,6 +1047,166 @@ public class LockPatternUtils { return nextAlarm; } + /** + * @return A formatted string of the next calendar event with a reminder + * (for showing on the lock screen), or null if there is no next event + * within a certain look-ahead time. + */ + public String[] getNextCalendarAlarm(long lookahead, String[] calendars, + boolean remindersOnly) { + long now = System.currentTimeMillis(); + long later = now + lookahead; + + StringBuilder where = new StringBuilder(); + if (remindersOnly) { + where.append(CalendarContract.Events.HAS_ALARM + "=1"); + } + if (calendars != null && calendars.length > 0) { + if (remindersOnly) { + where.append(" AND "); + } + where.append(CalendarContract.Events.CALENDAR_ID + " in ("); + for (int i = 0; i < calendars.length; i++) { + where.append(calendars[i]); + if (i != calendars.length - 1) { + where.append(","); + } + } + where.append(") "); + } + + // Projection array + String[] projection = new String[] { + CalendarContract.Events.TITLE, + CalendarContract.Instances.BEGIN, + CalendarContract.Events.DESCRIPTION, + CalendarContract.Events.EVENT_LOCATION, + CalendarContract.Events.ALL_DAY + }; + + // The indices for the projection array + int TITLE_INDEX = 0; + int BEGIN_TIME_INDEX = 1; + int DESCRIPTION_INDEX = 2; + int LOCATION_INDEX = 3; + int ALL_DAY_INDEX = 4; + + Uri uri = Uri.withAppendedPath(CalendarContract.Instances.CONTENT_URI, + String.format("%d/%d", now, later)); + String[] nextCalendarAlarm = new String[2]; + Cursor cursor = null; + + try { + cursor = mContentResolver.query(uri, projection, + where.toString(), null, "begin ASC"); + + if (cursor != null && cursor.moveToFirst()) { + + String title = cursor.getString(TITLE_INDEX); + long begin = cursor.getLong(BEGIN_TIME_INDEX); + String description = cursor.getString(DESCRIPTION_INDEX); + String location = cursor.getString(LOCATION_INDEX); + boolean allDay = cursor.getInt(ALL_DAY_INDEX) != 0; + + // Check the next event in the case of all day event. As UTC is used for all day + // events, the next event may be the one that actually starts sooner + if (allDay && !cursor.isLast()) { + cursor.moveToNext(); + long nextBegin = cursor.getLong(BEGIN_TIME_INDEX); + if (nextBegin < begin + TimeZone.getDefault().getOffset(begin)) { + title = cursor.getString(TITLE_INDEX); + begin = nextBegin; + description = cursor.getString(DESCRIPTION_INDEX); + location = cursor.getString(LOCATION_INDEX); + allDay = cursor.getInt(ALL_DAY_INDEX) != 0; + } + } + + // Set the event title as the first array item + nextCalendarAlarm[0] = title.toString(); + + // Start building the event details string + // Starting with the date + Date start = new Date(begin); + StringBuilder sb = new StringBuilder(); + + if (allDay) { + SimpleDateFormat sdf = new SimpleDateFormat( + mContext.getString(R.string.abbrev_wday_month_day_no_year)); + // Calendar stores all-day events in UTC -- setting the time zone ensures + // the correct date is shown. + sdf.setTimeZone(TimeZone.getTimeZone(Time.TIMEZONE_UTC)); + sb.append(sdf.format(start)); + } else { + sb.append(DateFormat.format("E", start)); + sb.append(" "); + sb.append(DateFormat.getTimeFormat(mContext).format(start)); + } + + // Add the event location if it should be shown + int showLocation = Settings.System.getInt(mContext.getContentResolver(), + Settings.System.LOCKSCREEN_CALENDAR_SHOW_LOCATION, 0); + if (showLocation != 0 && !TextUtils.isEmpty(location)) { + switch(showLocation) { + case 1: + // Show first line + int end = location.indexOf('\n'); + if(end == -1) { + sb.append(": " + location); + } else { + sb.append(": " + location.substring(0, end)); + } + break; + case 2: + // Show all + sb.append(": " + location); + break; + } + } + + // Add the event description if it should be shown + int showDescription = Settings.System.getInt(mContext.getContentResolver(), + Settings.System.LOCKSCREEN_CALENDAR_SHOW_DESCRIPTION, 0); + if (showDescription != 0 && !TextUtils.isEmpty(description)) { + + // Show the appropriate separator + if (showLocation == 0) { + sb.append(": "); + } else { + sb.append(" - "); + } + + switch(showDescription) { + case 1: + // Show first line + int end = description.indexOf('\n'); + if(end == -1) { + sb.append(description); + } else { + sb.append(description.substring(0, end)); + } + break; + case 2: + // Show all + sb.append(description); + break; + } + } + + // Set the time, location and description as the second array item + nextCalendarAlarm[1] = sb.toString(); + } + } catch (Exception e) { + // Do nothing + } finally { + if (cursor != null) { + cursor.close(); + } + } + + return nextCalendarAlarm; + } + private boolean getBoolean(String secureSettingKey, boolean defaultValue) { try { return getLockSettings().getBoolean(secureSettingKey, defaultValue, @@ -1346,4 +1515,13 @@ public class LockPatternUtils { return false; } + /** + * @hide + * Set the lock-before-unlock option (show widgets before the secure + * unlock screen). See config_enableLockBeforeUnlockScreen + */ + public void setLockBeforeUnlock(boolean enabled) { + setBoolean(Settings.Secure.LOCK_BEFORE_UNLOCK, enabled); + } + } -- cgit v1.1