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.magicsmoke;
18
19import android.app.Activity;
20import android.app.WallpaperManager;
21import android.app.WallpaperInfo;
22import android.app.Dialog;
23import android.service.wallpaper.IWallpaperConnection;
24import android.service.wallpaper.IWallpaperService;
25import android.service.wallpaper.IWallpaperEngine;
26import android.service.wallpaper.WallpaperSettingsActivity;
27import android.content.ServiceConnection;
28import android.content.Intent;
29import android.content.Context;
30import android.content.ComponentName;
31import android.content.SharedPreferences;
32import android.content.SharedPreferences.Editor;
33import android.os.RemoteException;
34import android.os.IBinder;
35import android.os.ParcelFileDescriptor;
36import android.os.Bundle;
37import android.view.MotionEvent;
38import android.view.View;
39import android.view.WindowManager;
40import android.view.ViewGroup;
41import android.view.Window;
42import android.view.LayoutInflater;
43import android.util.Log;
44import android.widget.TextView;
45
46public class MagicSmokeSelector extends Activity {
47
48    private static final String LOG_TAG = "MagicSmokeSelector";
49
50    private WallpaperManager mWallpaperManager;
51    private WallpaperConnection mWallpaperConnection;
52
53    private Intent mWallpaperIntent;
54    private SharedPreferences mSharedPref;
55    private int mCurrentPreset;
56
57    @Override
58    protected void onCreate(Bundle savedInstanceState) {
59        super.onCreate(savedInstanceState);
60
61        setContentView(R.layout.selector);
62
63        mWallpaperIntent = new Intent(this, MagicSmoke.class);
64
65        mWallpaperManager = WallpaperManager.getInstance(this);
66        mWallpaperConnection = new WallpaperConnection(mWallpaperIntent);
67
68        mSharedPref = getSharedPreferences("magicsmoke", Context.MODE_PRIVATE);
69        mCurrentPreset = mSharedPref.getInt("preset", MagicSmokeRS.DEFAULT_PRESET);
70        if (mCurrentPreset >= MagicSmokeRS.mPreset.length) {
71            mCurrentPreset = 0;
72            updatePrefs();
73        }
74    }
75
76    // button hook
77    public void setLiveWallpaper(View v) {
78        finish();
79    }
80
81
82    @Override
83    public void onResume() {
84        super.onResume();
85        if (mWallpaperConnection != null && mWallpaperConnection.mEngine != null) {
86            try {
87                mWallpaperConnection.mEngine.setVisibility(true);
88            } catch (RemoteException e) {
89                // Ignore
90            }
91        }
92    }
93
94    @Override
95    public void onPause() {
96        super.onPause();
97        if (mWallpaperConnection != null && mWallpaperConnection.mEngine != null) {
98            try {
99                mWallpaperConnection.mEngine.setVisibility(false);
100            } catch (RemoteException e) {
101                // Ignore
102            }
103        }
104    }
105
106    @Override
107    public void onAttachedToWindow() {
108        super.onAttachedToWindow();
109
110        if (!mWallpaperConnection.connect()) {
111            mWallpaperConnection = null;
112        }
113    }
114
115    @Override
116    public void onDetachedFromWindow() {
117        super.onDetachedFromWindow();
118
119        if (mWallpaperConnection != null) {
120            mWallpaperConnection.disconnect();
121        }
122        mWallpaperConnection = null;
123    }
124
125    @Override
126    public boolean onTouchEvent(MotionEvent event) {
127        // TODO: make this conditional on preview mode. Right now we
128        // don't get touch events in preview mode.
129        switch(event.getAction()) {
130            case MotionEvent.ACTION_DOWN:
131                if (mCurrentPreset == 0) {
132                    mCurrentPreset = MagicSmokeRS.mPreset.length - 1;
133                } else {
134                    mCurrentPreset--;
135                }
136                updatePrefs();
137                return true;
138        }
139
140        return super.onTouchEvent(event);
141    }
142
143    private void updatePrefs() {
144
145        Editor edit = mSharedPref.edit();
146        edit.putInt("preset", mCurrentPreset);
147        edit.commit();
148
149    }
150
151    class WallpaperConnection extends IWallpaperConnection.Stub implements ServiceConnection {
152        final Intent mIntent;
153        IWallpaperService mService;
154        IWallpaperEngine mEngine;
155        boolean mConnected;
156
157        WallpaperConnection(Intent intent) {
158            mIntent = intent;
159        }
160
161        public boolean connect() {
162            synchronized (this) {
163                if (!bindService(mIntent, this, Context.BIND_AUTO_CREATE)) {
164                    return false;
165                }
166
167                mConnected = true;
168                return true;
169            }
170        }
171
172        public void disconnect() {
173            synchronized (this) {
174                mConnected = false;
175                if (mEngine != null) {
176                    try {
177                        mEngine.destroy();
178                    } catch (RemoteException e) {
179                        // Ignore
180                    }
181                    mEngine = null;
182                }
183                unbindService(this);
184                mService = null;
185            }
186        }
187
188        public void onServiceConnected(ComponentName name, IBinder service) {
189            if (mWallpaperConnection == this) {
190                mService = IWallpaperService.Stub.asInterface(service);
191                try {
192                    final View view = findViewById(R.id.backgroundview);
193                    final View root = view.getRootView();
194                    mService.attach(this, view.getWindowToken(),
195                            WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY,
196                            true, root.getWidth(), root.getHeight());
197                } catch (RemoteException e) {
198                    Log.w(LOG_TAG, "Failed attaching wallpaper; clearing", e);
199                }
200            }
201        }
202
203        public void onServiceDisconnected(ComponentName name) {
204            mService = null;
205            mEngine = null;
206            if (mWallpaperConnection == this) {
207                Log.w(LOG_TAG, "Wallpaper service gone: " + name);
208            }
209        }
210
211        public void attachEngine(IWallpaperEngine engine) {
212            synchronized (this) {
213                if (mConnected) {
214                    mEngine = engine;
215                    try {
216                        engine.setVisibility(true);
217                    } catch (RemoteException e) {
218                        // Ignore
219                    }
220                } else {
221                    try {
222                        engine.destroy();
223                    } catch (RemoteException e) {
224                        // Ignore
225                    }
226                }
227            }
228        }
229
230        public ParcelFileDescriptor setWallpaper(String name) {
231            return null;
232        }
233    }
234}
235