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 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 @Override 77 public void onResume() { 78 super.onResume(); 79 if (mWallpaperConnection != null && mWallpaperConnection.mEngine != null) { 80 try { 81 mWallpaperConnection.mEngine.setVisibility(true); 82 } catch (RemoteException e) { 83 // Ignore 84 } 85 } 86 } 87 88 @Override 89 public void onPause() { 90 super.onPause(); 91 if (mWallpaperConnection != null && mWallpaperConnection.mEngine != null) { 92 try { 93 mWallpaperConnection.mEngine.setVisibility(false); 94 } catch (RemoteException e) { 95 // Ignore 96 } 97 } 98 } 99 100 @Override 101 public void onAttachedToWindow() { 102 super.onAttachedToWindow(); 103 104 if (!mWallpaperConnection.connect()) { 105 mWallpaperConnection = null; 106 } 107 } 108 109 @Override 110 public void onDetachedFromWindow() { 111 super.onDetachedFromWindow(); 112 113 if (mWallpaperConnection != null) { 114 mWallpaperConnection.disconnect(); 115 } 116 mWallpaperConnection = null; 117 } 118 119 @Override 120 public boolean onTouchEvent(MotionEvent event) { 121 // TODO: make this conditional on preview mode. Right now we 122 // don't get touch events in preview mode. 123 switch(event.getAction()) { 124 case MotionEvent.ACTION_DOWN: 125 if (mCurrentPreset == 0) { 126 mCurrentPreset = MagicSmokeRS.mPreset.length - 1; 127 } else { 128 mCurrentPreset--; 129 } 130 updatePrefs(); 131 return true; 132 } 133 134 return super.onTouchEvent(event); 135 } 136 137 private void updatePrefs() { 138 Editor edit = mSharedPref.edit(); 139 edit.putInt("preset", mCurrentPreset); 140 edit.apply(); 141 } 142 143 class WallpaperConnection extends IWallpaperConnection.Stub implements ServiceConnection { 144 final Intent mIntent; 145 IWallpaperService mService; 146 IWallpaperEngine mEngine; 147 boolean mConnected; 148 149 WallpaperConnection(Intent intent) { 150 mIntent = intent; 151 } 152 153 public boolean connect() { 154 synchronized (this) { 155 if (!bindService(mIntent, this, Context.BIND_AUTO_CREATE)) { 156 return false; 157 } 158 159 mConnected = true; 160 return true; 161 } 162 } 163 164 public void disconnect() { 165 synchronized (this) { 166 mConnected = false; 167 if (mEngine != null) { 168 try { 169 mEngine.destroy(); 170 } catch (RemoteException e) { 171 // Ignore 172 } 173 mEngine = null; 174 } 175 unbindService(this); 176 mService = null; 177 } 178 } 179 180 public void onServiceConnected(ComponentName name, IBinder service) { 181 if (mWallpaperConnection == this) { 182 mService = IWallpaperService.Stub.asInterface(service); 183 try { 184 final View view = findViewById(R.id.backgroundview); 185 final View root = view.getRootView(); 186 mService.attach(this, view.getWindowToken(), 187 WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY, 188 true, root.getWidth(), root.getHeight()); 189 } catch (RemoteException e) { 190 Log.w(LOG_TAG, "Failed attaching wallpaper; clearing", e); 191 } 192 } 193 } 194 195 public void onServiceDisconnected(ComponentName name) { 196 mService = null; 197 mEngine = null; 198 if (mWallpaperConnection == this) { 199 Log.w(LOG_TAG, "Wallpaper service gone: " + name); 200 } 201 } 202 203 public void attachEngine(IWallpaperEngine engine) { 204 synchronized (this) { 205 if (mConnected) { 206 mEngine = engine; 207 try { 208 engine.setVisibility(true); 209 } catch (RemoteException e) { 210 // Ignore 211 } 212 } else { 213 try { 214 engine.destroy(); 215 } catch (RemoteException e) { 216 // Ignore 217 } 218 } 219 } 220 } 221 222 public ParcelFileDescriptor setWallpaper(String name) { 223 return null; 224 } 225 226 public void engineShown(IWallpaperEngine engine) throws RemoteException { 227 } 228 } 229 } 230