Add browseable samples for Clockwork Beryl

Bug: 17473824
Change-Id: Id4c637733c491ac71bb6e269f55939c082fb0994
This commit is contained in:
Trevor Johns
2014-10-22 20:10:29 -07:00
parent 4a5ce9aedf
commit f420ff0d80
797 changed files with 34843 additions and 0 deletions

View File

@@ -0,0 +1,217 @@
/*
* Copyright (C) 2014 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.example.android.timer;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.provider.AlarmClock;
import android.support.wearable.view.WearableListView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.android.timer.util.Constants;
import com.example.android.timer.util.TimerFormat;
/** This class sets a timer. */
public class SetTimerActivity extends Activity implements WearableListView.ClickListener {
public static final int NUMBER_OF_TIMES = 10;
public static final String TAG = "SetTimerActivity";
private ListViewItem[] mTimeOptions = new ListViewItem[NUMBER_OF_TIMES];
private WearableListView mWearableListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int paramLength = getIntent().getIntExtra(AlarmClock.EXTRA_LENGTH, 0);
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "SetTimerActivity:onCreate=" + paramLength);
}
if (paramLength > 0 && paramLength <= 86400) {
long durationMillis = paramLength * 1000;
setupTimer(durationMillis);
finish();
return;
}
Resources res = getResources();
for (int i = 0; i < NUMBER_OF_TIMES; i++) {
mTimeOptions[i] = new ListViewItem(
res.getQuantityString(R.plurals.timer_minutes, i + 1, i + 1),
(i + 1) * 60 * 1000);
}
setContentView(R.layout.timer_set_timer);
// Initialize a simple list of countdown time options.
mWearableListView = (WearableListView) findViewById(R.id.times_list_view);
mWearableListView.setAdapter(new TimerWearableListViewAdapter(this));
mWearableListView.setClickListener(this);
}
/**
* Sets up an alarm (and an associated notification) to go off after <code>duration</code>
* milliseconds.
*/
private void setupTimer(long duration) {
NotificationManager notifyMgr =
((NotificationManager) getSystemService(NOTIFICATION_SERVICE));
// Delete dataItem and cancel a potential old countdown.
cancelCountdown(notifyMgr);
// Build notification and set it.
notifyMgr.notify(Constants.NOTIFICATION_TIMER_COUNTDOWN, buildNotification(duration));
// Register with the alarm manager to display a notification when the timer is done.
registerWithAlarmManager(duration);
finish();
}
@Override
public void onClick(WearableListView.ViewHolder holder) {
long duration = mTimeOptions[holder.getPosition()].duration;
setupTimer(duration);
}
@Override
public void onTopEmptyRegionClick() {
}
private void registerWithAlarmManager(long duration) {
// Get the alarm manager.
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// Create intent that gets fired when timer expires.
Intent intent = new Intent(Constants.ACTION_SHOW_ALARM, null, this,
TimerNotificationService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Calculate the time when it expires.
long wakeupTime = System.currentTimeMillis() + duration;
// Schedule an alarm.
alarm.setExact(AlarmManager.RTC_WAKEUP, wakeupTime, pendingIntent);
}
/**
* Build a notification including different actions and other various setup and return it.
*
* @param duration the duration of the timer.
* @return the notification to display.
*/
private Notification buildNotification(long duration) {
// Intent to restart a timer.
Intent restartIntent = new Intent(Constants.ACTION_RESTART_ALARM, null, this,
TimerNotificationService.class);
PendingIntent pendingIntentRestart = PendingIntent
.getService(this, 0, restartIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Intent to delete a timer.
Intent deleteIntent = new Intent(Constants.ACTION_DELETE_ALARM, null, this,
TimerNotificationService.class);
PendingIntent pendingIntentDelete = PendingIntent
.getService(this, 0, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Create countdown notification using a chronometer style.
return new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_cc_alarm)
.setContentTitle(getString(R.string.timer_time_left))
.setContentText(TimerFormat.getTimeString(duration))
.setUsesChronometer(true)
.setWhen(System.currentTimeMillis() + duration)
.addAction(R.drawable.ic_cc_alarm, getString(R.string.timer_restart),
pendingIntentRestart)
.addAction(R.drawable.ic_cc_alarm, getString(R.string.timer_delete),
pendingIntentDelete)
.setDeleteIntent(pendingIntentDelete)
.setLocalOnly(true)
.build();
}
/**
* Cancels an old countdown and deletes the dataItem.
*
* @param notifyMgr the notification manager.
*/
private void cancelCountdown(NotificationManager notifyMgr) {
notifyMgr.cancel(Constants.NOTIFICATION_TIMER_EXPIRED);
}
/** Model class for the listview. */
private static class ListViewItem {
// Duration in milliseconds.
long duration;
// Label to display.
private String label;
public ListViewItem(String label, long duration) {
this.label = label;
this.duration = duration;
}
@Override
public String toString() {
return label;
}
}
private final class TimerWearableListViewAdapter extends WearableListView.Adapter {
private final Context mContext;
private final LayoutInflater mInflater;
private TimerWearableListViewAdapter(Context context) {
mContext = context;
mInflater = LayoutInflater.from(context);
}
@Override
public WearableListView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new WearableListView.ViewHolder(
mInflater.inflate(R.layout.timer_list_item, null));
}
@Override
public void onBindViewHolder(WearableListView.ViewHolder holder, int position) {
TextView view = (TextView) holder.itemView.findViewById(R.id.time_text);
view.setText(mTimeOptions[position].label);
holder.itemView.setTag(position);
}
@Override
public int getItemCount() {
return NUMBER_OF_TIMES;
}
}
}

View File

@@ -0,0 +1,119 @@
/*
* Copyright (C) 2014 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.example.android.timer;
import android.app.AlarmManager;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.example.android.timer.util.Constants;
/**
* Service class that manages notifications of the timer.
*/
public class TimerNotificationService extends IntentService {
public static final String TAG = "TimerNotificationSvc";
public TimerNotificationService() {
super(TAG);
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
protected void onHandleIntent(Intent intent) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "onHandleIntent called with intent: " + intent);
}
String action = intent.getAction();
if (Constants.ACTION_SHOW_ALARM.equals(action)) {
showTimerDoneNotification();
} else if (Constants.ACTION_DELETE_ALARM.equals(action)) {
deleteTimer();
} else if (Constants.ACTION_RESTART_ALARM.equals(action)) {
restartAlarm();
} else {
throw new IllegalStateException("Undefined constant used: " + action);
}
}
private void restartAlarm() {
Intent dialogIntent = new Intent(this, SetTimerActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Timer restarted.");
}
}
private void deleteTimer() {
cancelCountdownNotification();
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(Constants.ACTION_SHOW_ALARM, null, this,
TimerNotificationService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
alarm.cancel(pendingIntent);
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Timer deleted.");
}
}
private void cancelCountdownNotification() {
NotificationManager notifyMgr =
((NotificationManager) getSystemService(NOTIFICATION_SERVICE));
notifyMgr.cancel(Constants.NOTIFICATION_TIMER_COUNTDOWN);
}
private void showTimerDoneNotification() {
// Cancel the countdown notification to show the "timer done" notification.
cancelCountdownNotification();
// Create an intent to restart a timer.
Intent restartIntent = new Intent(Constants.ACTION_RESTART_ALARM, null, this,
TimerNotificationService.class);
PendingIntent pendingIntentRestart = PendingIntent
.getService(this, 0, restartIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Create notification that timer has expired.
NotificationManager notifyMgr =
((NotificationManager) getSystemService(NOTIFICATION_SERVICE));
Notification notif = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_cc_alarm)
.setContentTitle(getString(R.string.timer_done))
.setContentText(getString(R.string.timer_done))
.setUsesChronometer(true)
.setWhen(System.currentTimeMillis())
.addAction(R.drawable.ic_cc_alarm, getString(R.string.timer_restart),
pendingIntentRestart)
.setLocalOnly(true)
.build();
notifyMgr.notify(Constants.NOTIFICATION_TIMER_EXPIRED, notif);
}
}

View File

@@ -0,0 +1,90 @@
/*
* Copyright (C) 2014 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.example.android.timer;
import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.support.wearable.view.WearableListView;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class WearableListItemLayout extends LinearLayout implements WearableListView.Item {
private final float mFadedTextAlpha;
private final int mFadedCircleColor;
private final int mChosenCircleColor;
private ImageView mCircle;
private float mScale;
private TextView mName;
public WearableListItemLayout(Context context) {
this(context, null);
}
public WearableListItemLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public WearableListItemLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mFadedTextAlpha = getResources().getInteger(R.integer.action_text_faded_alpha) / 100f;
mFadedCircleColor = getResources().getColor(R.color.wl_gray);
mChosenCircleColor = getResources().getColor(R.color.wl_blue);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mCircle = (ImageView) findViewById(R.id.circle);
mName = (TextView) findViewById(R.id.time_text);
}
@Override
public float getProximityMinValue() {
return 1f;
}
@Override
public float getProximityMaxValue() {
return 1.6f;
}
@Override
public float getCurrentProximityValue() {
return mScale;
}
@Override
public void setScalingAnimatorValue(float scale) {
mScale = scale;
mCircle.setScaleX(scale);
mCircle.setScaleY(scale);
}
@Override
public void onScaleUpStart() {
mName.setAlpha(1f);
((GradientDrawable) mCircle.getDrawable()).setColor(mChosenCircleColor);
}
@Override
public void onScaleDownStart() {
((GradientDrawable) mCircle.getDrawable()).setColor(mFadedCircleColor);
mName.setAlpha(mFadedTextAlpha);
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright (C) 2014 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.example.android.timer.util;
import android.net.Uri;
/** Used to hold constants. */
public final class Constants {
public static final String START_TIME = "timer_start_time";
public static final String ORIGINAL_TIME = "timer_original_time";
public static final String DATA_ITEM_PATH = "/timer";
public static final Uri URI_PATTERN_DATA_ITEMS =
Uri.fromParts("wear", DATA_ITEM_PATH, null);
public static final int NOTIFICATION_TIMER_COUNTDOWN = 1;
public static final int NOTIFICATION_TIMER_EXPIRED = 2;
public static final String ACTION_SHOW_ALARM
= "com.android.example.clockwork.timer.ACTION_SHOW";
public static final String ACTION_DELETE_ALARM
= "com.android.example.clockwork.timer.ACTION_DELETE";
public static final String ACTION_RESTART_ALARM
= "com.android.example.clockwork.timer.ACTION_RESTART";
private Constants() {
}
}

View File

@@ -0,0 +1,121 @@
/*
* Copyright (C) 2014 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.example.android.timer.util;
/** Helper class to format the timer. Based on com.android.deskclock.timer.CountingTimerView. */
public final class TimerFormat {
private static final String TWO_DIGITS = "%02d";
private static final String ONE_DIGIT = "%01d";
private static final String NEG_TWO_DIGITS = "-%02d";
private static final String NEG_ONE_DIGIT = "-%01d";
private static String mHours;
private static String mMinutes;
private static String mSeconds;
private TimerFormat() {
}
/**
* Update the time to display. Separates that time into the hours, minutes, seconds.
* Copied and shortened from com.android.deskclock.timer.CountingTimerView.
*
* @param time new time to display - in milliseconds
*/
private static void setTime(long time) {
boolean neg = false;
boolean showNeg = false;
String format;
if (time < 0) {
time = -time;
neg = showNeg = true;
}
long seconds = time / 1000;
long hundreds = (time - seconds * 1000) / 10;
long minutes = seconds / 60;
seconds = seconds - minutes * 60;
long hours = minutes / 60;
minutes = minutes - hours * 60;
if (hours > 999) {
hours = 0;
}
// The time can be between 0 and -1 seconds, but the "truncated" equivalent time of hours
// and minutes and seconds could be zero, so since we do not show fractions of seconds
// when counting down, do not show the minus sign.
if (hours == 0 && minutes == 0 && seconds == 0) {
showNeg = false;
}
// Normalize and check if it is 'time' to invalidate
if (!neg && hundreds != 0) {
seconds++;
if (seconds == 60) {
seconds = 0;
minutes++;
if (minutes == 60) {
minutes = 0;
hours++;
}
}
}
// Hours may be empty
if (hours >= 10) {
format = showNeg ? NEG_TWO_DIGITS : TWO_DIGITS;
mHours = String.format(format, hours);
} else if (hours > 0) {
format = showNeg ? NEG_ONE_DIGIT : ONE_DIGIT;
mHours = String.format(format, hours);
} else {
mHours = null;
}
// Minutes are never empty and when hours are non-empty, must be two digits
if (minutes >= 10 || hours > 0) {
format = (showNeg && hours == 0) ? NEG_TWO_DIGITS : TWO_DIGITS;
mMinutes = String.format(format, minutes);
} else {
format = (showNeg && hours == 0) ? NEG_ONE_DIGIT : ONE_DIGIT;
mMinutes = String.format(format, minutes);
}
// Seconds are always two digits
mSeconds = String.format(TWO_DIGITS, seconds);
}
/**
* Based on com.android.deskclock.timer.CountingTimerView.
*
* @param time the time to format.
* @return nicely formatted time.
*/
public static String getTimeString(long time) {
setTime(time);
if (mHours == null) {
return String.format("%s:%s", mMinutes, mSeconds);
}
return String.format("%s:%s:%s", mHours, mMinutes, mSeconds);
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright (C) 2014 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.example.android.timer.util;
import android.os.SystemClock;
/** This class represents a timer. */
public class TimerObj {
// Start time in milliseconds.
public long startTime;
// Length of the timer in milliseconds.
public long originalLength;
/**
* Construct a timer with a specific start time and length.
*
* @param startTime the start time of the timer.
* @param timerLength the length of the timer.
*/
public TimerObj(long startTime, long timerLength) {
this.startTime = startTime;
this.originalLength = timerLength;
}
/**
* Calculate the time left of this timer.
* @return the time left for this timer.
*/
public long timeLeft() {
long millis = SystemClock.elapsedRealtime();
return originalLength - (millis - startTime);
}
}