Home | History | Annotate | Download | only in wallpaper
      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.wallpaper;
     18 
     19 import android.service.wallpaper.WallpaperService;
     20 import android.os.Bundle;
     21 import android.renderscript.RenderScriptGL;
     22 import android.renderscript.RenderScript;
     23 import android.view.SurfaceHolder;
     24 import android.view.Surface;
     25 
     26 public abstract class RenderScriptWallpaper<T extends RenderScriptScene> extends WallpaperService {
     27     public Engine onCreateEngine() {
     28         return new RenderScriptEngine();
     29     }
     30 
     31     protected abstract T createScene(int width, int height);
     32 
     33     private class RenderScriptEngine extends Engine {
     34         private RenderScriptGL mRs;
     35         private T mRenderer;
     36 
     37         @Override
     38         public void onCreate(SurfaceHolder surfaceHolder) {
     39             super.onCreate(surfaceHolder);
     40             setTouchEventsEnabled(false);
     41             surfaceHolder.setSizeFromLayout();
     42         }
     43 
     44         @Override
     45         public void onDestroy() {
     46             super.onDestroy();
     47             destroyRenderer();
     48         }
     49 
     50         private void destroyRenderer() {
     51             if (mRenderer != null) {
     52                 mRenderer.stop();
     53                 mRenderer = null;
     54             }
     55             if (mRs != null) {
     56                 mRs.setSurface(null, 0, 0);
     57                 mRs.destroy();
     58                 mRs = null;
     59             }
     60         }
     61 
     62         @Override
     63         public void onVisibilityChanged(boolean visible) {
     64             super.onVisibilityChanged(visible);
     65             if (mRenderer != null) {
     66                 if (visible) {
     67                     mRenderer.start();
     68                 } else {
     69                     mRenderer.stop();
     70                 }
     71             }
     72         }
     73 
     74         @Override
     75         public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
     76             super.onSurfaceChanged(holder, format, width, height);
     77             if (mRs != null) {
     78                 mRs.setSurface(holder, width, height);
     79             }
     80             if (mRenderer == null) {
     81                 mRenderer = createScene(width, height);
     82                 mRenderer.init(mRs, getResources(), isPreview());
     83                 mRenderer.start();
     84             } else {
     85                 mRenderer.resize(width, height);
     86             }
     87         }
     88 
     89         @Override
     90         public void onOffsetsChanged(float xOffset, float yOffset,
     91                 float xStep, float yStep, int xPixels, int yPixels) {
     92             if (mRenderer != null) mRenderer.setOffset(xOffset, yOffset, xPixels, yPixels);
     93         }
     94 
     95         @Override
     96         public void onSurfaceCreated(SurfaceHolder holder) {
     97             super.onSurfaceCreated(holder);
     98 
     99             RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
    100             mRs = new RenderScriptGL(RenderScriptWallpaper.this, sc);
    101             mRs.setPriority(RenderScript.Priority.LOW);
    102         }
    103 
    104         @Override
    105         public void onSurfaceDestroyed(SurfaceHolder holder) {
    106             super.onSurfaceDestroyed(holder);
    107             destroyRenderer();
    108         }
    109 
    110         @Override
    111         public Bundle onCommand(String action, int x, int y, int z,
    112                 Bundle extras, boolean resultRequested) {
    113             if (mRenderer != null) {
    114                 return mRenderer.onCommand(action, x, y, z, extras, resultRequested);
    115             } else {
    116                 return null;
    117             }
    118         }
    119 
    120     }
    121 }
    122