2 * Copyright (C) 2009 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.deskclock;
19 import android.app.Activity;
20 import android.app.AlertDialog;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.content.Intent;
25 import android.content.IntentFilter;
26 import android.content.SharedPreferences;
27 import android.content.res.Configuration;
28 import android.database.Cursor;
29 import android.graphics.drawable.Drawable;
30 import android.graphics.drawable.ColorDrawable;
31 import android.net.Uri;
32 import android.os.Bundle;
33 import android.os.Handler;
34 import android.provider.Settings;
35 import android.text.TextUtils;
36 import android.util.Log;
37 import android.view.animation.AnimationUtils;
38 import android.view.ContextMenu;
39 import android.view.ContextMenu.ContextMenuInfo;
40 import android.view.LayoutInflater;
41 import android.view.Menu;
42 import android.view.MenuItem;
43 import android.view.View;
44 import android.view.View.OnClickListener;
45 import android.view.View.OnCreateContextMenuListener;
46 import android.view.ViewGroup;
47 import android.view.Window;
48 import android.view.WindowManager;
49 import android.widget.AdapterView;
50 import android.widget.AdapterView.AdapterContextMenuInfo;
51 import android.widget.AdapterView.OnItemClickListener;
52 import android.widget.Button;
53 import android.widget.TextView;
54 import android.widget.CheckBox;
56 import static android.os.BatteryManager.BATTERY_STATUS_CHARGING;
57 import static android.os.BatteryManager.BATTERY_STATUS_FULL;
58 import static android.os.BatteryManager.BATTERY_STATUS_UNKNOWN;
60 import java.text.DateFormat;
61 import java.util.Date;
64 * DeskClock clock view for desk docks.
66 public class DeskClock extends Activity {
68 private static final String LOG_TAG = "DeskClock";
70 private static final String MUSIC_NOW_PLAYING_ACTIVITY = "com.android.music.PLAYBACK_VIEWER";
72 private TextView mNextAlarm = null;
73 private TextView mDate;
74 private TextView mBatteryDisplay;
75 private DigitalClock mTime;
77 private boolean mDimmed = false;
79 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
81 public void onReceive(Context context, Intent intent) {
82 final String action = intent.getAction();
83 if (Intent.ACTION_DATE_CHANGED.equals(action)) {
85 } else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
87 intent.getIntExtra("status", BATTERY_STATUS_UNKNOWN),
88 intent.getIntExtra("level", 0));
93 private DateFormat mDateFormat;
95 private int mBatteryLevel;
96 private boolean mPluggedIn;
98 // Adapted from KeyguardUpdateMonitor.java
99 private void handleBatteryUpdate(int plugStatus, int batteryLevel) {
100 final boolean pluggedIn = (plugStatus == BATTERY_STATUS_CHARGING || plugStatus == BATTERY_STATUS_FULL);
101 if (pluggedIn != mPluggedIn || batteryLevel != mBatteryLevel) {
102 mBatteryLevel = batteryLevel;
103 mPluggedIn = pluggedIn;
108 private void refreshBattery() {
109 if (mPluggedIn /* || mBatteryLevel < LOW_BATTERY_THRESHOLD */) {
110 mBatteryDisplay.setCompoundDrawablesWithIntrinsicBounds(
111 0, 0, android.R.drawable.ic_lock_idle_charging, 0);
112 mBatteryDisplay.setText(
113 getString(R.string.battery_charging_level, mBatteryLevel));
114 mBatteryDisplay.setVisibility(View.VISIBLE);
116 mBatteryDisplay.setVisibility(View.INVISIBLE);
120 private void refreshDate() {
121 mDate.setText(mDateFormat.format(new Date()));
124 private void refreshAlarm() {
125 String nextAlarm = Settings.System.getString(getContentResolver(),
126 Settings.System.NEXT_ALARM_FORMATTED);
127 if (!TextUtils.isEmpty(nextAlarm)) {
128 mNextAlarm.setText(nextAlarm);
129 //mNextAlarm.setCompoundDrawablesWithIntrinsicBounds(
130 // android.R.drawable.ic_lock_idle_alarm, 0, 0, 0);
131 mNextAlarm.setVisibility(View.VISIBLE);
133 mNextAlarm.setVisibility(View.INVISIBLE);
137 private void doDim() {
138 View tintView = findViewById(R.id.window_tint);
140 Window win = getWindow();
141 WindowManager.LayoutParams winParams = win.getAttributes();
143 // dim the wallpaper somewhat (how much is determined below)
144 winParams.flags |= (WindowManager.LayoutParams.FLAG_DIM_BEHIND);
147 winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
148 // winParams.flags &= (~WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
149 winParams.dimAmount = 0.5f; // pump up contrast in dim mode
151 // show the window tint
152 tintView.startAnimation(AnimationUtils.loadAnimation(this, R.anim.dim));
154 winParams.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
155 // winParams.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
156 winParams.dimAmount = 0.2f; // lower contrast in normal mode
158 // hide the window tint
159 tintView.startAnimation(AnimationUtils.loadAnimation(this, R.anim.undim));
162 win.setAttributes(winParams);
166 public void onResume() {
169 // reload the date format in case the user has changed settings
171 mDateFormat = java.text.DateFormat.getDateInstance(java.text.DateFormat.FULL);
173 IntentFilter filter = new IntentFilter();
174 filter.addAction(Intent.ACTION_DATE_CHANGED);
175 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
176 registerReceiver(mIntentReceiver, filter);
185 public void onPause() {
187 unregisterReceiver(mIntentReceiver);
191 private void initViews() {
192 setContentView(R.layout.desk_clock);
194 mTime = (DigitalClock) findViewById(R.id.time);
195 mDate = (TextView) findViewById(R.id.date);
196 mBatteryDisplay = (TextView) findViewById(R.id.battery);
198 mNextAlarm = (TextView) findViewById(R.id.nextAlarm);
200 final Button alarmButton = (Button) findViewById(R.id.alarm_button);
201 alarmButton.setOnClickListener(new View.OnClickListener() {
202 public void onClick(View v) {
203 startActivity(new Intent(DeskClock.this, AlarmClock.class));
207 final Button galleryButton = (Button) findViewById(R.id.gallery_button);
208 galleryButton.setOnClickListener(new View.OnClickListener() {
209 public void onClick(View v) {
211 startActivity(new Intent(
213 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI));
214 } catch (android.content.ActivityNotFoundException e) {
215 Log.e(LOG_TAG, "Couldn't launch image browser", e);
220 final Button musicButton = (Button) findViewById(R.id.music_button);
221 musicButton.setOnClickListener(new View.OnClickListener() {
222 public void onClick(View v) {
224 startActivity(new Intent(MUSIC_NOW_PLAYING_ACTIVITY));
225 } catch (android.content.ActivityNotFoundException e) {
226 Log.e(LOG_TAG, "Couldn't launch music browser", e);
231 final Button homeButton = (Button) findViewById(R.id.home_button);
232 homeButton.setOnClickListener(new View.OnClickListener() {
233 public void onClick(View v) {
235 new Intent(Intent.ACTION_MAIN)
236 .addCategory(Intent.CATEGORY_HOME));
240 final Button nightmodeButton = (Button) findViewById(R.id.nightmode_button);
241 nightmodeButton.setOnClickListener(new View.OnClickListener() {
242 public void onClick(View v) {
252 public void onConfigurationChanged(Configuration newConfig) {
253 super.onConfigurationChanged(newConfig);
259 protected void onCreate(Bundle icicle) {
260 super.onCreate(icicle);