diff options
author | Danny Baumann <dannybaumann@web.de> | 2014-11-11 10:44:49 +0100 |
---|---|---|
committer | Michael Bestas <mikeioannina@gmail.com> | 2015-10-25 11:55:22 +0200 |
commit | 09618116991575a0bd725dcaa3434c72d1bf8060 (patch) | |
tree | cac4d3e0eaaa714d173c75f04d686deb12fd2152 /src | |
parent | fe0bf1fbbba83e8906c3062184fe4d1dd04eac8e (diff) | |
download | android_packages_wallpapers_PhaseBeam-09618116991575a0bd725dcaa3434c72d1bf8060.tar.gz android_packages_wallpapers_PhaseBeam-09618116991575a0bd725dcaa3434c72d1bf8060.tar.bz2 android_packages_wallpapers_PhaseBeam-09618116991575a0bd725dcaa3434c72d1bf8060.zip |
Bring over PhaseBeam recoloring from cm-11.0 branch.
Change-Id: Idea935647ce4aa4c1e60657c071b6bd921712bce
Diffstat (limited to 'src')
-rw-r--r-- | src/com/android/phasebeam/PhaseBeamRS.java | 57 | ||||
-rw-r--r-- | src/com/android/phasebeam/PhaseBeamSelector.java | 289 | ||||
-rw-r--r-- | src/com/android/phasebeam/PhaseBeamView.java | 3 | ||||
-rw-r--r-- | src/com/android/phasebeam/PhaseBeamWallpaper.java | 9 | ||||
-rw-r--r-- | src/com/android/phasebeam/phasebeam.rs | 82 |
5 files changed, 403 insertions, 37 deletions
diff --git a/src/com/android/phasebeam/PhaseBeamRS.java b/src/com/android/phasebeam/PhaseBeamRS.java index bd0687b..6963b4c 100644 --- a/src/com/android/phasebeam/PhaseBeamRS.java +++ b/src/com/android/phasebeam/PhaseBeamRS.java @@ -3,6 +3,8 @@ package com.android.phasebeam; import static android.renderscript.Sampler.Value.NEAREST; import static android.renderscript.Sampler.Value.WRAP; +import android.content.Context; +import android.content.SharedPreferences; import android.content.res.Resources; import android.renderscript.Allocation; import android.renderscript.Matrix4f; @@ -29,7 +31,7 @@ import java.io.IOException; import java.util.ArrayList; import android.util.Log; -public class PhaseBeamRS { +public class PhaseBeamRS implements SharedPreferences.OnSharedPreferenceChangeListener { public static String LOG_TAG = "PhaseBeam"; public static final int DOT_COUNT = 28; private Resources mRes; @@ -51,18 +53,27 @@ public class PhaseBeamRS { private ScriptField_VertexColor_s mVertexColors; private int mDensityDPI; + private SharedPreferences mSharedPref; + private Context mContext; + private boolean mCanScroll; boolean mInited = false; - public void init(int dpi, RenderScriptGL rs, Resources res, int width, int height) { + public void init(Context context, int dpi, RenderScriptGL rs, + Resources res, int width, int height) { if (!mInited) { mDensityDPI = dpi; + mContext = context; + mSharedPref = mContext.getSharedPreferences(PhaseBeamSelector.KEY_PREFS, + Context.MODE_PRIVATE); + mSharedPref.registerOnSharedPreferenceChangeListener(this); mRS = rs; mRes = res; mWidth = width; mHeight = height; + mCanScroll = mRes.getBoolean(R.bool.scrolling_enabled); mDotParticles = new ScriptField_Particle(mRS, DOT_COUNT); Mesh.AllocationBuilder smb2 = new Mesh.AllocationBuilder(mRS); @@ -96,10 +107,41 @@ public class PhaseBeamRS { mRS.bindRootScript(mScript); mScript.invoke_positionParticles(); + makeNewState(); + mInited = true; } } + public void uninit() { + mSharedPref.unregisterOnSharedPreferenceChangeListener(this); + } + + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + if (key.equals(PhaseBeamSelector.KEY_ENABLED)) { + loadTextures(); + } + makeNewState(); + } + + private void makeNewState() { + Float3 adjust; + if (mSharedPref.getBoolean(PhaseBeamSelector.KEY_ENABLED, + mRes.getBoolean(R.bool.recolor_enabled))) { + adjust = new Float3( + mSharedPref.getFloat(PhaseBeamSelector.KEY_HUE, + Float.valueOf(mRes.getString(R.string.hue))), + mSharedPref.getFloat(PhaseBeamSelector.KEY_SATURATION, + Float.valueOf(mRes.getString(R.string.saturation))), + mSharedPref.getFloat(PhaseBeamSelector.KEY_BRIGHTNESS, + Float.valueOf(mRes.getString(R.string.brightness)))); + } else { + adjust = new Float3(-1.0f, 1.0f, 1.0f); + } + mScript.set_adjust(adjust); + } + private Matrix4f getProjectionNormalized(int w, int h) { // range -1,1 in the narrow axis at z = 0. Matrix4f m1 = new Matrix4f(); @@ -165,6 +207,8 @@ public class PhaseBeamRS { float blue = new Float(values[4]); mVertexColors.set_position(i, new Float3(xPos, yPos, 0.0f), false); mVertexColors.set_color(i, new Float4(red, green, blue, 1.0f), false); + mVertexColors.set_realColor(i, new Float4(red, green, blue, 1.0f), false); + mVertexColors.set_adjust(i, new Float3(-1.0f, 1.0f, 1.0f), false); } mVertexColors.copyAll(); @@ -183,8 +227,9 @@ public class PhaseBeamRS { } private void loadTextures() { - mDotAllocation = loadTexture(R.drawable.dot); - mBeamAllocation = loadTexture(R.drawable.beam); + boolean recolor = mSharedPref.getBoolean(PhaseBeamSelector.KEY_ENABLED, false); + mDotAllocation = loadTexture(recolor ? R.drawable.dot_grey : R.drawable.dot); + mBeamAllocation = loadTexture(recolor ? R.drawable.beam_grey : R.drawable.beam); mScript.set_textureDot(mDotAllocation); mScript.set_textureBeam(mBeamAllocation); } @@ -246,7 +291,9 @@ public class PhaseBeamRS { } public void setOffset(float xOffset, float yOffset, int xPixels, int yPixels) { - mScript.set_xOffset(xOffset); + if (mCanScroll) { + mScript.set_xOffset(xOffset); + } } public void resize(int w, int h) { diff --git a/src/com/android/phasebeam/PhaseBeamSelector.java b/src/com/android/phasebeam/PhaseBeamSelector.java new file mode 100644 index 0000000..f5df837 --- /dev/null +++ b/src/com/android/phasebeam/PhaseBeamSelector.java @@ -0,0 +1,289 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.phasebeam; + +import android.app.Activity; +import android.app.WallpaperManager; +import android.app.WallpaperInfo; +import android.app.Dialog; +import android.service.wallpaper.IWallpaperConnection; +import android.service.wallpaper.IWallpaperService; +import android.service.wallpaper.IWallpaperEngine; +import android.service.wallpaper.WallpaperSettingsActivity; +import android.content.ServiceConnection; +import android.content.Intent; +import android.content.Context; +import android.content.ComponentName; +import android.content.SharedPreferences; +import android.graphics.Rect; +import android.os.RemoteException; +import android.os.IBinder; +import android.os.ParcelFileDescriptor; +import android.os.Bundle; +import android.util.Log; +import android.view.MotionEvent; +import android.view.View; +import android.view.WindowManager; +import android.view.ViewGroup; +import android.view.Window; +import android.view.LayoutInflater; +import android.widget.CheckBox; +import android.widget.CompoundButton; +import android.widget.SeekBar; + +public class PhaseBeamSelector extends Activity implements + CompoundButton.OnCheckedChangeListener, SeekBar.OnSeekBarChangeListener { + private static final String LOG_TAG = "PhaseBeamSelector"; + + public static final String KEY_PREFS = "phasebeam"; + public static final String KEY_ENABLED = "enabled"; + public static final String KEY_HUE = "hue"; + public static final String KEY_SATURATION = "saturation"; + public static final String KEY_BRIGHTNESS = "brightness"; + + private static final float HUE_RANGE_MIN = 0.0f; + private static final float HUE_RANGE_MAX = 1.0f; + private static final float SATURATION_RANGE_MIN = 0.0f; + private static final float SATURATION_RANGE_MAX = 1.0f; + private static final float BRIGHTNESS_RANGE_MIN = 0.5f; + private static final float BRIGHTNESS_RANGE_MAX = 1.5f; + + private WallpaperManager mWallpaperManager; + private WallpaperConnection mWallpaperConnection; + private Intent mWallpaperIntent; + + private SharedPreferences mSharedPref; + + private CheckBox mEnableBox; + private SeekBar mColorSeekBar; + private SeekBar mSaturationSeekBar; + private SeekBar mBrightnessSeekBar; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.selector); + + mWallpaperIntent = new Intent(this, PhaseBeamWallpaper.class); + mWallpaperManager = WallpaperManager.getInstance(this); + mWallpaperConnection = new WallpaperConnection(mWallpaperIntent); + + mSharedPref = getSharedPreferences(KEY_PREFS, Context.MODE_PRIVATE); + + mEnableBox = (CheckBox) findViewById(R.id.recolor); + mEnableBox.setOnCheckedChangeListener(this); + mColorSeekBar = (SeekBar) findViewById(R.id.hue); + mColorSeekBar.setOnSeekBarChangeListener(this); + mSaturationSeekBar = (SeekBar) findViewById(R.id.saturation); + mSaturationSeekBar.setOnSeekBarChangeListener(this); + mBrightnessSeekBar = (SeekBar) findViewById(R.id.brightness); + mBrightnessSeekBar.setOnSeekBarChangeListener(this); + } + + @Override + public void onResume() { + super.onResume(); + if (mWallpaperConnection != null && mWallpaperConnection.mEngine != null) { + try { + mWallpaperConnection.mEngine.setVisibility(true); + } catch (RemoteException e) { + // Ignore + } + } + updateUiFromPrefs(); + } + + @Override + public void onPause() { + super.onPause(); + if (mWallpaperConnection != null && mWallpaperConnection.mEngine != null) { + try { + mWallpaperConnection.mEngine.setVisibility(false); + } catch (RemoteException e) { + // Ignore + } + } + } + + @Override + public void onAttachedToWindow() { + super.onAttachedToWindow(); + + if (!mWallpaperConnection.connect()) { + mWallpaperConnection = null; + } + } + + @Override + public void onDetachedFromWindow() { + super.onDetachedFromWindow(); + + if (mWallpaperConnection != null) { + mWallpaperConnection.disconnect(); + } + mWallpaperConnection = null; + } + + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + mColorSeekBar.setEnabled(isChecked); + mSaturationSeekBar.setEnabled(isChecked); + mBrightnessSeekBar.setEnabled(isChecked); + mSharedPref.edit().putBoolean(KEY_ENABLED, isChecked).apply(); + } + + @Override + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + if (!fromUser) { + return; + } + + float min, max; + String key; + + if (seekBar == mColorSeekBar) { + min = HUE_RANGE_MIN; + max = HUE_RANGE_MAX; + key = KEY_HUE; + } else if (seekBar == mSaturationSeekBar) { + min = SATURATION_RANGE_MIN; + max = SATURATION_RANGE_MAX; + key = KEY_SATURATION; + } else { + min = BRIGHTNESS_RANGE_MIN; + max = BRIGHTNESS_RANGE_MAX; + key = KEY_BRIGHTNESS; + } + + float value = ((max - min) * progress / seekBar.getMax()) + min; + mSharedPref.edit().putFloat(key, value).apply(); + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) { + // Nothing to do here + } + + @Override + public void onStopTrackingTouch(SeekBar seekBar) { + // Nothing to do here + } + + private void updateUiFromPrefs() { + mEnableBox.setChecked(mSharedPref.getBoolean(KEY_ENABLED, false)); + + updateSeekBarFromFloat(mColorSeekBar, mSharedPref.getFloat(KEY_HUE, 0.0f), + HUE_RANGE_MIN, HUE_RANGE_MAX); + updateSeekBarFromFloat(mSaturationSeekBar, mSharedPref.getFloat(KEY_SATURATION, 1.0f), + SATURATION_RANGE_MIN, SATURATION_RANGE_MAX); + updateSeekBarFromFloat(mBrightnessSeekBar, mSharedPref.getFloat(KEY_BRIGHTNESS, 1.0f), + BRIGHTNESS_RANGE_MIN, BRIGHTNESS_RANGE_MAX); + } + + private void updateSeekBarFromFloat(SeekBar seekBar, float value, float min, float max) { + float progress = (value - min) * seekBar.getMax() / (max - min); + seekBar.setProgress((int) progress); + } + + class WallpaperConnection extends IWallpaperConnection.Stub implements ServiceConnection { + final Intent mIntent; + IWallpaperService mService; + IWallpaperEngine mEngine; + boolean mConnected; + + WallpaperConnection(Intent intent) { + mIntent = intent; + } + + public boolean connect() { + synchronized (this) { + if (!bindService(mIntent, this, Context.BIND_AUTO_CREATE)) { + return false; + } + + mConnected = true; + return true; + } + } + + public void disconnect() { + synchronized (this) { + mConnected = false; + if (mEngine != null) { + try { + mEngine.destroy(); + } catch (RemoteException e) { + // Ignore + } + mEngine = null; + } + unbindService(this); + mService = null; + } + } + + public void onServiceConnected(ComponentName name, IBinder service) { + if (mWallpaperConnection == this) { + mService = IWallpaperService.Stub.asInterface(service); + try { + final View view = findViewById(R.id.backgroundview); + final View root = view.getRootView(); + mService.attach(this, view.getWindowToken(), + WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY, + true, root.getWidth(), root.getHeight(), new Rect()); + } catch (RemoteException e) { + Log.w(LOG_TAG, "Failed attaching wallpaper; clearing", e); + } + } + } + + public void onServiceDisconnected(ComponentName name) { + mService = null; + mEngine = null; + if (mWallpaperConnection == this) { + Log.w(LOG_TAG, "Wallpaper service gone: " + name); + } + } + + public void attachEngine(IWallpaperEngine engine) { + synchronized (this) { + if (mConnected) { + mEngine = engine; + try { + engine.setVisibility(true); + } catch (RemoteException e) { + // Ignore + } + } else { + try { + engine.destroy(); + } catch (RemoteException e) { + // Ignore + } + } + } + } + + public ParcelFileDescriptor setWallpaper(String name) { + return null; + } + + public void engineShown(IWallpaperEngine engine) throws RemoteException { + } + } +} diff --git a/src/com/android/phasebeam/PhaseBeamView.java b/src/com/android/phasebeam/PhaseBeamView.java index e88a0b1..27689f2 100644 --- a/src/com/android/phasebeam/PhaseBeamView.java +++ b/src/com/android/phasebeam/PhaseBeamView.java @@ -25,7 +25,7 @@ public class PhaseBeamView extends RSSurfaceView { mRS.setSurface(holder, w, h); mRender = new PhaseBeamRS(); - mRender.init(240, mRS, getResources(), w, h); + mRender.init(getContext(), 240, mRS, getResources(), w, h); } } @@ -36,6 +36,7 @@ public class PhaseBeamView extends RSSurfaceView { mRS.setSurface(null, 0, 0); mRS = null; destroyRenderScriptGL(); + mRender.uninit(); } } diff --git a/src/com/android/phasebeam/PhaseBeamWallpaper.java b/src/com/android/phasebeam/PhaseBeamWallpaper.java index 2541ab2..0f51fc1 100644 --- a/src/com/android/phasebeam/PhaseBeamWallpaper.java +++ b/src/com/android/phasebeam/PhaseBeamWallpaper.java @@ -44,6 +44,7 @@ public class PhaseBeamWallpaper extends WallpaperService { public void destroyRenderer() { if (mWallpaperRS != null) { mWallpaperRS.stop(); + mWallpaperRS.uninit(); mWallpaperRS = null; } @@ -80,7 +81,8 @@ public class PhaseBeamWallpaper extends WallpaperService { if (mWallpaperRS == null) { mWallpaperRS = new PhaseBeamRS(); - mWallpaperRS.init(mDensityDPI, mRenderScript, getResources(), width, height); + mWallpaperRS.init(PhaseBeamWallpaper.this, mDensityDPI, + mRenderScript, getResources(), width, height); mWallpaperRS.start(); } @@ -108,8 +110,7 @@ public class PhaseBeamWallpaper extends WallpaperService { @Override public void onOffsetsChanged(float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset) { - // TODO: Uncomment this once we can work out framerate issues - //mWallpaperRS.setOffset(xOffset, yOffset, xPixelOffset, yPixelOffset); + mWallpaperRS.setOffset(xOffset, yOffset, xPixelOffset, yPixelOffset); } } -}
\ No newline at end of file +} diff --git a/src/com/android/phasebeam/phasebeam.rs b/src/com/android/phasebeam/phasebeam.rs index ccbf60a..9f8ac26 100644 --- a/src/com/android/phasebeam/phasebeam.rs +++ b/src/com/android/phasebeam/phasebeam.rs @@ -22,6 +22,7 @@ static int numVertColors; typedef struct __attribute__((packed, aligned(4))) Particle { float3 position; float offsetX; + float3 adjust; } Particle_t; typedef struct VpConsts { @@ -34,6 +35,8 @@ typedef struct VertexColor_s { float3 position; float offsetX; float4 color; + float4 realColor; + float3 adjust; } VertexColor; VertexColor* vertexColors; @@ -43,6 +46,8 @@ rs_mesh dotMesh; rs_mesh beamMesh; rs_mesh gBackgroundMesh; +float3 adjust = { -1.0, 1.0, 1.0 }; +float3 oldAdjust = { -1.0, 1.0, 1.0 }; float densityDPI; float xOffset = 0.5; @@ -56,6 +61,11 @@ static float halfScreenHeight; static float newOffset = 0.5; static float oldOffset = 0.5; +static const float zxParticleSpeed = 0.0000780; +static const float zxBeamSpeed = 0.00005; +static const float yzParticleSpeed = 0.00011; +static const float yzBeamSpeed = 0.000080; + void positionParticles() { screenWidth = rsgGetWidth(); screenHeight = rsgGetHeight(); @@ -85,6 +95,7 @@ void positionParticles() { } particle->position.z = z; particle->offsetX = 0; + particle->adjust = adjust; particle++; } @@ -102,19 +113,34 @@ void positionParticles() { beamParticles->position.z = z; beamParticles->offsetX = 0; + beamParticles->adjust = adjust; beamParticles++; } } int root() { + float speedbump; newOffset = xOffset*2; + speedbump = newOffset != oldOffset ? 0.25 : 1.0; rsgClearColor(0.0f, 0.f, 0.f,1.0f); - if(newOffset != oldOffset) { + if(newOffset != oldOffset + || oldAdjust.x != adjust.x + || oldAdjust.y != adjust.y + || oldAdjust.z != adjust.z) { VertexColor* vert = vertexColors; + bool useAdjust = adjust.x >= 0; for(int i=0; i<numVertColors; i++) { vert->offsetX = -xOffset/2.0; + vert->realColor = vert->color; + if (useAdjust) { + float grey = 0.3 * vert->color.x + 0.59 * vert->color.y + 0.11 * vert->color.z; + vert->realColor.x = grey; + vert->realColor.y = grey; + vert->realColor.z = grey; + } + vert->adjust = adjust; vert++; } } @@ -127,36 +153,38 @@ int root() { Particle_t* beam = beamParticles; Particle_t* particle = dotParticles; - for(int i=0; i<numDotParticles; i++) { + for (int i=0; i<numBeamParticles; i++) { + if(beam->position.x/beam->position.z > 0.5) { + beam->position.x = -1.0; + } + if(beam->position.y > 1.15) { + beam->position.y = -1.15; + beam->position.x = rsRand(-1.25f, 1.25f); + } else { + beam->position.y += yzBeamSpeed * beam->position.z * speedbump; + } + beam->position.x += zxBeamSpeed * beam->position.z * speedbump; + beam->offsetX = newOffset; + beam->adjust = adjust; + beam++; + } - if(newOffset==oldOffset) { - if(beam->position.x/beam->position.z > 0.5) { - beam->position.x = -1.0; - } - if(particle->position.x/particle->position.z > 0.5) { - particle->position.x = -1.0; - } + for(int i=0; i<numDotParticles; i++) { + if(particle->position.x/particle->position.z > 0.5) { + particle->position.x = -1.0; + } - if(beam->position.y > 1.05) { - beam->position.y = -1.05; - beam->position.x = rsRand(-1.25f, 1.25f); - } else { - beam->position.y = beam->position.y + 0.000160*beam->position.z; - } - if(particle->position.y > 1.25) { - particle->position.y = -1.25; - particle->position.x = rsRand(0.0f, 3.0f); + if(particle->position.y > 1.25) { + particle->position.y = -1.25; + particle->position.x = rsRand(0.0f, 3.0f); - } else { - particle->position.y = particle->position.y + 0.00022*particle->position.z; - } + } else { + particle->position.y += yzParticleSpeed * particle->position.z * speedbump; } - beam->position.x = beam->position.x + 0.0001*beam->position.z; - beam->offsetX = newOffset; - beam++; particle->offsetX = newOffset; - particle->position.x = particle->position.x + 0.0001560*beam->position.z; + particle->position.x += zxParticleSpeed * particle->position.z * speedbump; + particle->adjust = adjust; particle++; } @@ -170,7 +198,7 @@ int root() { rsgDrawMesh(dotMesh); oldOffset = newOffset; + oldAdjust = adjust; - return 66; - + return 66 * speedbump; } |