• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.magicsmoke;
18 
19 import android.app.Activity;
20 import android.app.WallpaperManager;
21 import android.app.WallpaperInfo;
22 import android.app.Dialog;
23 import android.service.wallpaper.IWallpaperConnection;
24 import android.service.wallpaper.IWallpaperService;
25 import android.service.wallpaper.IWallpaperEngine;
26 import android.service.wallpaper.WallpaperSettingsActivity;
27 import android.content.ServiceConnection;
28 import android.content.Intent;
29 import android.content.Context;
30 import android.content.ComponentName;
31 import android.content.SharedPreferences;
32 import android.content.SharedPreferences.Editor;
33 import android.os.RemoteException;
34 import android.os.IBinder;
35 import android.os.ParcelFileDescriptor;
36 import android.os.Bundle;
37 import android.view.MotionEvent;
38 import android.view.View;
39 import android.view.WindowManager;
40 import android.view.ViewGroup;
41 import android.view.Window;
42 import android.view.LayoutInflater;
43 import android.util.Log;
44 import android.widget.TextView;
45 
46 public 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
onCreate(Bundle savedInstanceState)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
setLiveWallpaper(View v)77     public void setLiveWallpaper(View v) {
78         finish();
79     }
80 
81 
82     @Override
onResume()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
onPause()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
onAttachedToWindow()107     public void onAttachedToWindow() {
108         super.onAttachedToWindow();
109 
110         if (!mWallpaperConnection.connect()) {
111             mWallpaperConnection = null;
112         }
113     }
114 
115     @Override
onDetachedFromWindow()116     public void onDetachedFromWindow() {
117         super.onDetachedFromWindow();
118 
119         if (mWallpaperConnection != null) {
120             mWallpaperConnection.disconnect();
121         }
122         mWallpaperConnection = null;
123     }
124 
125     @Override
onTouchEvent(MotionEvent event)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 
updatePrefs()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 
WallpaperConnection(Intent intent)157         WallpaperConnection(Intent intent) {
158             mIntent = intent;
159         }
160 
connect()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 
disconnect()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 
onServiceConnected(ComponentName name, IBinder service)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 
onServiceDisconnected(ComponentName name)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 
attachEngine(IWallpaperEngine engine)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 
setWallpaper(String name)230         public ParcelFileDescriptor setWallpaper(String name) {
231             return null;
232         }
233     }
234 }
235