Home | History | Annotate | Download | only in magicsmoke
      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     // 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         Editor edit = mSharedPref.edit();
    145         edit.putInt("preset", mCurrentPreset);
    146         edit.apply();
    147     }
    148 
    149     class WallpaperConnection extends IWallpaperConnection.Stub implements ServiceConnection {
    150         final Intent mIntent;
    151         IWallpaperService mService;
    152         IWallpaperEngine mEngine;
    153         boolean mConnected;
    154 
    155         WallpaperConnection(Intent intent) {
    156             mIntent = intent;
    157         }
    158 
    159         public boolean connect() {
    160             synchronized (this) {
    161                 if (!bindService(mIntent, this, Context.BIND_AUTO_CREATE)) {
    162                     return false;
    163                 }
    164 
    165                 mConnected = true;
    166                 return true;
    167             }
    168         }
    169 
    170         public void disconnect() {
    171             synchronized (this) {
    172                 mConnected = false;
    173                 if (mEngine != null) {
    174                     try {
    175                         mEngine.destroy();
    176                     } catch (RemoteException e) {
    177                         // Ignore
    178                     }
    179                     mEngine = null;
    180                 }
    181                 unbindService(this);
    182                 mService = null;
    183             }
    184         }
    185 
    186         public void onServiceConnected(ComponentName name, IBinder service) {
    187             if (mWallpaperConnection == this) {
    188                 mService = IWallpaperService.Stub.asInterface(service);
    189                 try {
    190                     final View view = findViewById(R.id.backgroundview);
    191                     final View root = view.getRootView();
    192                     mService.attach(this, view.getWindowToken(),
    193                             WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY,
    194                             true, root.getWidth(), root.getHeight());
    195                 } catch (RemoteException e) {
    196                     Log.w(LOG_TAG, "Failed attaching wallpaper; clearing", e);
    197                 }
    198             }
    199         }
    200 
    201         public void onServiceDisconnected(ComponentName name) {
    202             mService = null;
    203             mEngine = null;
    204             if (mWallpaperConnection == this) {
    205                 Log.w(LOG_TAG, "Wallpaper service gone: " + name);
    206             }
    207         }
    208 
    209         public void attachEngine(IWallpaperEngine engine) {
    210             synchronized (this) {
    211                 if (mConnected) {
    212                     mEngine = engine;
    213                     try {
    214                         engine.setVisibility(true);
    215                     } catch (RemoteException e) {
    216                         // Ignore
    217                     }
    218                 } else {
    219                     try {
    220                         engine.destroy();
    221                     } catch (RemoteException e) {
    222                         // Ignore
    223                     }
    224                 }
    225             }
    226         }
    227 
    228         public ParcelFileDescriptor setWallpaper(String name) {
    229             return null;
    230         }
    231     }
    232 }
    233