1 /* 2 * Copyright (C) 2008 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 17 package com.android.alarmclock; 18 19 import android.app.Activity; 20 import android.content.SharedPreferences; 21 import android.os.Bundle; 22 import android.view.LayoutInflater; 23 import android.view.View; 24 import android.view.ViewGroup; 25 import android.view.Window; 26 import android.widget.AdapterView; 27 import android.widget.BaseAdapter; 28 import android.widget.Gallery; 29 30 /** 31 * Clock face picker for the Alarm Clock application. 32 */ 33 public class ClockPicker extends Activity implements 34 AdapterView.OnItemSelectedListener, AdapterView.OnItemClickListener { 35 36 private LayoutInflater mFactory; 37 private Gallery mGallery; 38 39 private SharedPreferences mPrefs; 40 private View mClock; 41 private ViewGroup mClockLayout; 42 private int mPosition; 43 44 @Override onCreate(Bundle icicle)45 public void onCreate(Bundle icicle) { 46 super.onCreate(icicle); 47 requestWindowFeature(Window.FEATURE_NO_TITLE); 48 49 mFactory = LayoutInflater.from(this); 50 setContentView(R.layout.clockpicker); 51 52 mGallery = (Gallery) findViewById(R.id.gallery); 53 mGallery.setAdapter(new ClockAdapter()); 54 mGallery.setOnItemSelectedListener(this); 55 mGallery.setOnItemClickListener(this); 56 57 mPrefs = getSharedPreferences(AlarmClock.PREFERENCES, 0); 58 int face = mPrefs.getInt(AlarmClock.PREF_CLOCK_FACE, 0); 59 if (face < 0 || face >= AlarmClock.CLOCKS.length) face = 0; 60 61 mClockLayout = (ViewGroup) findViewById(R.id.clock_layout); 62 mClockLayout.setOnClickListener(new View.OnClickListener() { 63 public void onClick(View v) { 64 selectClock(mPosition); 65 } 66 }); 67 68 mGallery.setSelection(face, false); 69 } 70 onItemSelected(AdapterView parent, View v, int position, long id)71 public void onItemSelected(AdapterView parent, View v, int position, long id) { 72 if (mClock != null) { 73 mClockLayout.removeView(mClock); 74 } 75 mClock = mFactory.inflate(AlarmClock.CLOCKS[position], null); 76 mClockLayout.addView(mClock, 0); 77 mPosition = position; 78 } 79 onItemClick(AdapterView parent, View v, int position, long id)80 public void onItemClick(AdapterView parent, View v, int position, long id) { 81 selectClock(position); 82 } 83 selectClock(int position)84 private synchronized void selectClock(int position) { 85 SharedPreferences.Editor ed = mPrefs.edit(); 86 ed.putInt(AlarmClock.PREF_CLOCK_FACE, position); 87 ed.commit(); 88 89 setResult(RESULT_OK); 90 finish(); 91 } 92 onNothingSelected(AdapterView parent)93 public void onNothingSelected(AdapterView parent) { 94 } 95 96 class ClockAdapter extends BaseAdapter { 97 ClockAdapter()98 public ClockAdapter() { 99 } 100 getCount()101 public int getCount() { 102 return AlarmClock.CLOCKS.length; 103 } 104 getItem(int position)105 public Object getItem(int position) { 106 return position; 107 } 108 getItemId(int position)109 public long getItemId(int position) { 110 return position; 111 } 112 getView(final int position, View convertView, ViewGroup parent)113 public View getView(final int position, View convertView, ViewGroup parent) { 114 View clock = mFactory.inflate(AlarmClock.CLOCKS[position], null); 115 return clock; 116 } 117 118 } 119 } 120