1/*
2 * Copyright (C) 2006 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.example.android.home;
18
19import android.app.Activity;
20import android.content.Context;
21import android.os.Bundle;
22import android.util.Log;
23import android.view.MotionEvent;
24import android.view.View;
25import android.view.ViewGroup;
26import android.view.Window;
27import android.widget.AdapterView;
28import android.widget.BaseAdapter;
29import android.widget.Gallery;
30import android.widget.ImageView;
31import android.widget.Gallery.LayoutParams;
32
33import java.io.IOException;
34import java.io.InputStream;
35
36/**
37 * Wallpaper picker for the Home application. User can choose from
38 * a gallery of stock photos.
39 */
40public class Wallpaper extends Activity implements
41        AdapterView.OnItemSelectedListener, AdapterView.OnItemClickListener {
42
43    private static final String LOG_TAG = "Home";
44
45    private static final Integer[] THUMB_IDS = {
46            R.drawable.bg_android_icon,
47            R.drawable.bg_sunrise_icon,
48            R.drawable.bg_sunset_icon,
49    };
50
51    private static final Integer[] IMAGE_IDS = {
52            R.drawable.bg_android,
53            R.drawable.bg_sunrise,
54            R.drawable.bg_sunset,
55    };
56
57    private Gallery mGallery;
58    private boolean mIsWallpaperSet;
59
60    @Override
61    public void onCreate(Bundle icicle) {
62        super.onCreate(icicle);
63        requestWindowFeature(Window.FEATURE_NO_TITLE);
64
65        setContentView(R.layout.wallpaper);
66
67        mGallery = (Gallery) findViewById(R.id.gallery);
68        mGallery.setAdapter(new ImageAdapter(this));
69        mGallery.setOnItemSelectedListener(this);
70        mGallery.setOnItemClickListener(this);
71    }
72
73    @Override
74    protected void onResume() {
75        super.onResume();
76        mIsWallpaperSet = false;
77    }
78
79    public void onItemSelected(AdapterView parent, View v, int position, long id) {
80        getWindow().setBackgroundDrawableResource(IMAGE_IDS[position]);
81    }
82
83    public void onItemClick(AdapterView parent, View v, int position, long id) {
84        selectWallpaper(position);
85    }
86
87    /*
88     * When using touch if you tap an image it triggers both the onItemClick and
89     * the onTouchEvent causing the wallpaper to be set twice. Synchronize this
90     * method and ensure we only set the wallpaper once.
91     */
92    private synchronized void selectWallpaper(int position) {
93        if (mIsWallpaperSet) {
94            return;
95        }
96        mIsWallpaperSet = true;
97        try {
98            InputStream stream = getResources().openRawResource(IMAGE_IDS[position]);
99            setWallpaper(stream);
100            setResult(RESULT_OK);
101            finish();
102        } catch (IOException e) {
103            Log.e(LOG_TAG, "Failed to set wallpaper " + e);
104        }
105    }
106
107    public void onNothingSelected(AdapterView parent) {
108    }
109
110    @Override
111    public boolean onTouchEvent(MotionEvent event) {
112        selectWallpaper(mGallery.getSelectedItemPosition());
113        return true;
114    }
115
116    public class ImageAdapter extends BaseAdapter {
117
118        private Context mContext;
119
120        public ImageAdapter(Context c) {
121            mContext = c;
122        }
123
124        public int getCount() {
125            return THUMB_IDS.length;
126        }
127
128        public Object getItem(int position) {
129            return position;
130        }
131
132        public long getItemId(int position) {
133            return position;
134        }
135
136        public View getView(final int position, View convertView, ViewGroup parent) {
137            ImageView i = new ImageView(mContext);
138
139            i.setImageResource(THUMB_IDS[position]);
140            i.setAdjustViewBounds(true);
141            i.setLayoutParams(new Gallery.LayoutParams(
142                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
143            i.setBackgroundResource(android.R.drawable.picture_frame);
144            return i;
145        }
146
147    }
148
149}
150
151
152