add a widget config activity for changing widget alpha

This commit is contained in:
tibbi 2016-08-30 19:54:07 +02:00
parent ac8ef37edd
commit cecf2faac8
12 changed files with 207 additions and 0 deletions

View File

@ -27,6 +27,15 @@
</intent-filter>
</activity>
<activity
android:name=".activities.WidgetConfigureActivity"
android:screenOrientation="portrait"
android:theme="@style/MyWidgetConfigTheme">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</intent-filter>
</activity>
<activity
android:name=".activities.AboutActivity"
android:label="@string/about"

View File

@ -6,4 +6,5 @@ public class Constants {
public static final String IS_FIRST_RUN = "is_first_run";
public static final String IS_DARK_THEME = "is_dark_theme";
public static final String BRIGHT_DISPLAY = "bright_display";
public static final String WIDGET_COLOR = "widget_color";
}

View File

@ -0,0 +1,129 @@
package com.simplemobiletools.flashlight.activities;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RemoteViews;
import android.widget.SeekBar;
import com.simplemobiletools.flashlight.Constants;
import com.simplemobiletools.flashlight.MyWidgetProvider;
import com.simplemobiletools.flashlight.R;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class WidgetConfigureActivity extends AppCompatActivity {
@BindView(R.id.config_widget_seekbar) SeekBar mWidgetSeekBar;
@BindView(R.id.config_widget_color) View mWidgetColorPicker;
@BindView(R.id.config_image) ImageView mImage;
private static float mWidgetAlpha;
private static int mWidgetId;
private static int mWidgetColor;
private static int mWidgetColorWithoutTransparency;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setResult(RESULT_CANCELED);
setContentView(R.layout.widget_config);
ButterKnife.bind(this);
initVariables();
final Intent intent = getIntent();
final Bundle extras = intent.getExtras();
if (extras != null)
mWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
if (mWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
finish();
}
private void initVariables() {
final SharedPreferences prefs = getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE);
mWidgetColor = prefs.getInt(Constants.WIDGET_COLOR, 1);
if (mWidgetColor == 1) {
mWidgetColor = getResources().getColor(R.color.colorPrimary);
mWidgetAlpha = 1.f;
} else {
mWidgetAlpha = Color.alpha(mWidgetColor) / (float) 255;
}
mWidgetColorWithoutTransparency = Color.rgb(Color.red(mWidgetColor), Color.green(mWidgetColor), Color.blue(mWidgetColor));
mWidgetSeekBar.setOnSeekBarChangeListener(seekbarChangeListener);
mWidgetSeekBar.setProgress((int) (mWidgetAlpha * 100));
updateBackgroundColor();
}
@OnClick(R.id.config_save)
public void saveConfig() {
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
final RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget);
appWidgetManager.updateAppWidget(mWidgetId, views);
storeWidgetColors();
requestWidgetUpdate();
final Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
@OnClick(R.id.config_widget_color)
public void pickBackgroundColor() {
}
private void storeWidgetColors() {
final SharedPreferences prefs = getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE);
prefs.edit().putInt(Constants.WIDGET_COLOR, mWidgetColor).apply();
}
private void requestWidgetUpdate() {
final Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, MyWidgetProvider.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[]{mWidgetId});
sendBroadcast(intent);
}
private void updateBackgroundColor() {
mWidgetColor = adjustAlpha(mWidgetColorWithoutTransparency, mWidgetAlpha);
mWidgetColorPicker.setBackgroundColor(mWidgetColor);
mImage.getDrawable().mutate().setColorFilter(mWidgetColor, PorterDuff.Mode.SRC_IN);
}
private SeekBar.OnSeekBarChangeListener seekbarChangeListener = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mWidgetAlpha = (float) progress / (float) 100;
updateBackgroundColor();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
private int adjustAlpha(int color, float factor) {
final int alpha = Math.round(Color.alpha(color) * factor);
final int red = Color.red(color);
final int green = Color.green(color);
final int blue = Color.blue(color);
return Color.argb(alpha, red, green, blue);
}
}

View File

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="@dimen/activity_margin"
android:paddingBottom="@dimen/activity_margin">
<ImageView
android:id="@+id/config_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/config_widget_color"
android:scaleType="centerInside"
android:src="@mipmap/flashlight_small"/>
<Button
android:id="@+id/config_widget_color"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_above="@+id/config_save"/>
<RelativeLayout
android:id="@+id/config_widget_seekbar_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/config_widget_color"
android:layout_alignTop="@+id/config_widget_color"
android:layout_toRightOf="@+id/config_widget_color"
android:background="@android:color/white">
<SeekBar
android:id="@+id/config_widget_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"/>
</RelativeLayout>
<Button
android:id="@+id/config_save"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@color/translucent_black"
android:fontFamily="sans-serif-light"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:text="@string/ok"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/config_text_size"/>
</RelativeLayout>

View File

@ -2,6 +2,7 @@
<string name="app_name">Simple Flashlight</string>
<string name="app_launcher_name">Flashlight</string>
<string name="camera_error">Rilevamento fotocamera fallito</string>
<string name="ok">OK</string>
<!-- Settings -->
<string name="settings">Impostazioni</string>

View File

@ -2,6 +2,7 @@
<string name="app_name">シンプル フラッシュライト</string>
<string name="app_launcher_name">Flashlight</string>
<string name="camera_error">カメラの取得に失敗しました</string>
<string name="ok">OK</string>
<!-- Settings -->
<string name="settings">設定</string>

View File

@ -2,6 +2,7 @@
<string name="app_name">Simple Flashlight</string>
<string name="app_launcher_name">Flashlight</string>
<string name="camera_error">Det gick inte att komma åt kameran</string>
<string name="ok">OK</string>
<!-- Settings -->
<string name="settings">Inställningar</string>

View File

@ -4,4 +4,5 @@
<color name="colorPrimaryDark">#ffe27725</color>
<color name="colorAccent">@color/colorPrimary</color>
<color name="translucent_white">#aaffffff</color>
<color name="translucent_black">#88000000</color>
</resources>

View File

@ -6,4 +6,5 @@
<dimen name="settings_padding">8dp</dimen>
<dimen name="normal_text_size">14sp</dimen>
<dimen name="config_text_size">18sp</dimen>
</resources>

View File

@ -2,6 +2,7 @@
<string name="app_name">Simple Flashlight</string>
<string name="app_launcher_name">Flashlight</string>
<string name="camera_error">Obtaining the camera failed</string>
<string name="ok">OK</string>
<!-- Settings -->
<string name="settings">Settings</string>

View File

@ -25,6 +25,11 @@
<item name="android:windowBackground">@null</item>
</style>
<style name="MyWidgetConfigTheme" parent="@style/AppTheme">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowShowWallpaper">true</item>
</style>
<style name="AppTheme.ActionBarStyle" parent="@style/Base.Widget.AppCompat.ActionBar">
<item name="background">@color/colorPrimary</item>
<item name="titleTextStyle">@style/AppTheme.ActionBar.TitleTextStyle</item>

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:configure="com.simplemobiletools.flashlight.activities.WidgetConfigureActivity"
android:initialLayout="@layout/widget"
android:minHeight="40dp"
android:minWidth="40dp"/>