2 * Copyright (C) 2008 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.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;
31 * Clock face picker for the Alarm Clock application.
33 public class ClockPicker extends Activity implements
34 AdapterView.OnItemSelectedListener, AdapterView.OnItemClickListener {
36 private LayoutInflater mFactory;
37 private Gallery mGallery;
39 private SharedPreferences mPrefs;
41 private ViewGroup mClockLayout;
42 private int mPosition;
45 public void onCreate(Bundle icicle) {
46 super.onCreate(icicle);
47 requestWindowFeature(Window.FEATURE_NO_TITLE);
49 mFactory = LayoutInflater.from(this);
50 setContentView(R.layout.clockpicker);
52 mGallery = (Gallery) findViewById(R.id.gallery);
53 mGallery.setAdapter(new ClockAdapter());
54 mGallery.setOnItemSelectedListener(this);
55 mGallery.setOnItemClickListener(this);
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;
61 mClockLayout = (ViewGroup) findViewById(R.id.clock_layout);
62 mClockLayout.setOnClickListener(new View.OnClickListener() {
63 public void onClick(View v) {
64 selectClock(mPosition);
68 mGallery.setSelection(face, false);
71 public void onItemSelected(AdapterView parent, View v, int position, long id) {
73 mClockLayout.removeView(mClock);
75 mClock = mFactory.inflate(AlarmClock.CLOCKS[position], null);
76 mClockLayout.addView(mClock, 0);
80 public void onItemClick(AdapterView parent, View v, int position, long id) {
81 selectClock(position);
84 private synchronized void selectClock(int position) {
85 SharedPreferences.Editor ed = mPrefs.edit();
86 ed.putInt(AlarmClock.PREF_CLOCK_FACE, position);
93 public void onNothingSelected(AdapterView parent) {
96 class ClockAdapter extends BaseAdapter {
98 public ClockAdapter() {
101 public int getCount() {
102 return AlarmClock.CLOCKS.length;
105 public Object getItem(int position) {
109 public long getItemId(int position) {
113 public View getView(final int position, View convertView, ViewGroup parent) {
114 View clock = mFactory.inflate(AlarmClock.CLOCKS[position], null);