DeskClock.java revision 72a353a7d443f17a0e16412ae85ad359d97e9394
1/* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17package com.android.deskclock; 18 19import android.app.Activity; 20import android.app.AlertDialog; 21import android.content.BroadcastReceiver; 22import android.content.Context; 23import android.content.DialogInterface; 24import android.content.Intent; 25import android.content.IntentFilter; 26import android.content.SharedPreferences; 27import android.content.res.Configuration; 28import android.database.Cursor; 29import android.graphics.drawable.Drawable; 30import android.graphics.drawable.ColorDrawable; 31import android.net.Uri; 32import android.os.Bundle; 33import android.os.Handler; 34import android.provider.Settings; 35import android.text.TextUtils; 36import android.util.Log; 37import android.view.animation.AnimationUtils; 38import android.view.ContextMenu; 39import android.view.ContextMenu.ContextMenuInfo; 40import android.view.LayoutInflater; 41import android.view.Menu; 42import android.view.MenuItem; 43import android.view.View; 44import android.view.View.OnClickListener; 45import android.view.View.OnCreateContextMenuListener; 46import android.view.ViewGroup; 47import android.view.Window; 48import android.view.WindowManager; 49import android.widget.AdapterView; 50import android.widget.AdapterView.AdapterContextMenuInfo; 51import android.widget.AdapterView.OnItemClickListener; 52import android.widget.Button; 53import android.widget.TextView; 54import android.widget.CheckBox; 55 56import static android.os.BatteryManager.BATTERY_STATUS_CHARGING; 57import static android.os.BatteryManager.BATTERY_STATUS_FULL; 58import static android.os.BatteryManager.BATTERY_STATUS_UNKNOWN; 59 60import java.text.DateFormat; 61import java.util.Date; 62 63/** 64 * DeskClock clock view for desk docks. 65 */ 66public class DeskClock extends Activity { 67 68 private static final String LOG_TAG = "DeskClock"; 69 70 private static final String MUSIC_NOW_PLAYING_ACTIVITY = "com.android.music.PLAYBACK_VIEWER"; 71 72 private TextView mNextAlarm = null; 73 private TextView mDate; 74 private TextView mBatteryDisplay; 75 private DigitalClock mTime; 76 77 private boolean mDimmed = false; 78 79 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 80 @Override 81 public void onReceive(Context context, Intent intent) { 82 final String action = intent.getAction(); 83 if (Intent.ACTION_DATE_CHANGED.equals(action)) { 84 refreshDate(); 85 } else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) { 86 handleBatteryUpdate( 87 intent.getIntExtra("status", BATTERY_STATUS_UNKNOWN), 88 intent.getIntExtra("level", 0)); 89 } 90 } 91 }; 92 93 private DateFormat mDateFormat; 94 95 private int mBatteryLevel; 96 private boolean mPluggedIn; 97 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; 104 refreshBattery(); 105 } 106 } 107 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); 115 } else { 116 mBatteryDisplay.setVisibility(View.INVISIBLE); 117 } 118 } 119 120 private void refreshDate() { 121 mDate.setText(mDateFormat.format(new Date())); 122 } 123 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); 132 } else { 133 mNextAlarm.setVisibility(View.INVISIBLE); 134 } 135 } 136 137 private void doDim() { 138 View tintView = findViewById(R.id.window_tint); 139 140 Window win = getWindow(); 141 WindowManager.LayoutParams winParams = win.getAttributes(); 142 143 // dim the wallpaper somewhat (how much is determined below) 144 winParams.flags |= (WindowManager.LayoutParams.FLAG_DIM_BEHIND); 145 146 if (mDimmed) { 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 150 151 // show the window tint 152 tintView.startAnimation(AnimationUtils.loadAnimation(this, R.anim.dim)); 153 } else { 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 157 158 // hide the window tint 159 tintView.startAnimation(AnimationUtils.loadAnimation(this, R.anim.undim)); 160 } 161 162 win.setAttributes(winParams); 163 } 164 165 @Override 166 public void onResume() { 167 super.onResume(); 168 169 // reload the date format in case the user has changed settings 170 // recently 171 mDateFormat = java.text.DateFormat.getDateInstance(java.text.DateFormat.FULL); 172 173 IntentFilter filter = new IntentFilter(); 174 filter.addAction(Intent.ACTION_DATE_CHANGED); 175 filter.addAction(Intent.ACTION_BATTERY_CHANGED); 176 registerReceiver(mIntentReceiver, filter); 177 178 doDim(); 179 refreshDate(); 180 refreshAlarm(); 181 refreshBattery(); 182 } 183 184 @Override 185 public void onPause() { 186 super.onPause(); 187 unregisterReceiver(mIntentReceiver); 188 } 189 190 @Override 191 protected void onCreate(Bundle icicle) { 192 super.onCreate(icicle); 193 194 setContentView(R.layout.desk_clock); 195 196 mTime = (DigitalClock) findViewById(R.id.time); 197 mDate = (TextView) findViewById(R.id.date); 198 mBatteryDisplay = (TextView) findViewById(R.id.battery); 199 200 mNextAlarm = (TextView) findViewById(R.id.nextAlarm); 201 202 final Button alarmButton = (Button) findViewById(R.id.alarm_button); 203 alarmButton.setOnClickListener(new View.OnClickListener() { 204 public void onClick(View v) { 205 startActivity(new Intent(DeskClock.this, AlarmClock.class)); 206 } 207 }); 208 209 final Button galleryButton = (Button) findViewById(R.id.gallery_button); 210 galleryButton.setOnClickListener(new View.OnClickListener() { 211 public void onClick(View v) { 212 try { 213 startActivity(new Intent( 214 Intent.ACTION_VIEW, 215 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI)); 216 } catch (android.content.ActivityNotFoundException e) { 217 Log.e(LOG_TAG, "Couldn't launch image browser", e); 218 } 219 } 220 }); 221 222 final Button musicButton = (Button) findViewById(R.id.music_button); 223 musicButton.setOnClickListener(new View.OnClickListener() { 224 public void onClick(View v) { 225 try { 226 startActivity(new Intent(MUSIC_NOW_PLAYING_ACTIVITY)); 227 } catch (android.content.ActivityNotFoundException e) { 228 Log.e(LOG_TAG, "Couldn't launch music browser", e); 229 } 230 } 231 }); 232 233 final Button homeButton = (Button) findViewById(R.id.home_button); 234 homeButton.setOnClickListener(new View.OnClickListener() { 235 public void onClick(View v) { 236 startActivity( 237 new Intent(Intent.ACTION_MAIN) 238 .addCategory(Intent.CATEGORY_HOME)); 239 } 240 }); 241 242 final Button nightmodeButton = (Button) findViewById(R.id.nightmode_button); 243 nightmodeButton.setOnClickListener(new View.OnClickListener() { 244 public void onClick(View v) { 245 mDimmed = ! mDimmed; 246 doDim(); 247 } 248 }); 249 250 } 251 252} 253