Code cleanup

This commit is contained in:
ByteHamster 2019-10-23 20:19:22 +02:00
parent 28cc00a016
commit 6642b79ac3
5 changed files with 30 additions and 44 deletions

View File

@ -5,9 +5,8 @@ import android.app.WallpaperManager;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RemoteViews;
import androidx.appcompat.app.AppCompatActivity;
import android.appwidget.AppWidgetManager;
@ -16,7 +15,6 @@ import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.RemoteViews;
import android.widget.SeekBar;
import android.widget.TextView;
@ -24,11 +22,9 @@ import androidx.core.content.ContextCompat;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.receiver.PlayerWidget;
import de.danoeh.antennapod.core.service.PlayerWidgetJobService;
public class WidgetConfigActivity extends AppCompatActivity {
private static final String TAG = "WidgetConfigActivity";
private final int DEFAULT_COLOR = 0x00262C31;
private int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
private SeekBar opacitySeekBar;
@ -49,8 +45,8 @@ public class WidgetConfigActivity extends AppCompatActivity {
}
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetId);
setResult(RESULT_CANCELED,resultValue);
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
setResult(RESULT_CANCELED, resultValue);
if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
}
@ -59,12 +55,14 @@ public class WidgetConfigActivity extends AppCompatActivity {
opacityTextView = findViewById(R.id.widget_opacity_textView);
opacitySeekBar = findViewById(R.id.widget_opacity_seekBar);
widgetPreview = findViewById(R.id.widgetLayout);
findViewById(R.id.butConfirm).setOnClickListener(this::confirmCreateWidget);
opacitySeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
opacityTextView.setText(seekBar.getProgress() + "%");
widgetPreview.setBackgroundColor(getColorWithAlpha(DEFAULT_COLOR,opacitySeekBar.getProgress()));
int color = getColorWithAlpha(PlayerWidget.DEFAULT_COLOR, opacitySeekBar.getProgress());
widgetPreview.setBackgroundColor(color);
}
@Override
@ -88,29 +86,22 @@ public class WidgetConfigActivity extends AppCompatActivity {
}
}
public void confirmCreateWidget(View v) {
int backgroundColor = getColorWithAlpha(DEFAULT_COLOR,opacitySeekBar.getProgress());
private void confirmCreateWidget(View v) {
int backgroundColor = getColorWithAlpha(PlayerWidget.DEFAULT_COLOR, opacitySeekBar.getProgress());
RemoteViews views = new RemoteViews(this.getPackageName(), R.layout.player_widget);
views.setInt(R.id.widgetLayout,"setBackgroundColor",backgroundColor);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
appWidgetManager.updateAppWidget(appWidgetId,views);
SharedPreferences prefs = getSharedPreferences(PlayerWidget.WIDGET_PREFS, MODE_PRIVATE);
SharedPreferences prefs = getSharedPreferences(PlayerWidget.PREFS_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(PlayerWidget.WIDGET_COLOR + appWidgetId,backgroundColor);
editor.putInt(PlayerWidget.KEY_WIDGET_COLOR + appWidgetId, backgroundColor);
editor.apply();
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetId);
setResult(RESULT_OK,resultValue);
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
setResult(RESULT_OK, resultValue);
finish();
PlayerWidgetJobService.updateWidget(this);
}
public int getColorWithAlpha(int color,int opacity) {
Log.d(TAG,"Widget background color : " + ((int)Math.round(0xFF * (0.01 * opacity)) << 24 | color));
return (int)Math.round(0xFF * (0.01 * opacity)) * 0x1000000 + color ;
private int getColorWithAlpha(int color, int opacity) {
return (int) Math.round(0xFF * (0.01 * opacity)) * 0x1000000 + color;
}
}
}

View File

@ -69,11 +69,10 @@
android:progress="100" />
<Button
android:id="@+id/button"
android:id="@+id/butConfirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="confirmCreateWidget"
android:text="@string/widget_create_button" />
</LinearLayout>

View File

@ -13,11 +13,10 @@ import de.danoeh.antennapod.core.service.PlayerWidgetJobService;
public class PlayerWidget extends AppWidgetProvider {
private static final String TAG = "PlayerWidget";
private static final String PREFS_NAME = "PlayerWidgetPrefs";
public static final String PREFS_NAME = "PlayerWidgetPrefs";
private static final String KEY_ENABLED = "WidgetEnabled";
public static final String WIDGET_PREFS = "widget_preferences";
public static final String WIDGET_COLOR = "widget_color";
public static final String KEY_WIDGET_COLOR = "widget_color";
public static final int DEFAULT_COLOR = 0x00262C31;
@Override
public void onReceive(Context context, Intent intent) {
@ -49,12 +48,10 @@ public class PlayerWidget extends AppWidgetProvider {
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
Log.d(TAG,"OnDeleted");
Log.d(TAG, "OnDeleted");
for (int appWidgetId : appWidgetIds) {
SharedPreferences prefs = context.getSharedPreferences(WIDGET_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.remove(WIDGET_COLOR + appWidgetId);
Log.d(TAG, "Widget deleted ID " + appWidgetId);
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
prefs.edit().remove(KEY_WIDGET_COLOR + appWidgetId).apply();
}
super.onDeleted(context, appWidgetIds);
}

View File

@ -45,9 +45,6 @@ public class PlayerWidgetJobService extends SafeJobIntentService {
private final Object waitForService = new Object();
private final Object waitUsingService = new Object();
private static final String WIDGET_PREFS = "widget_preferences";
private static final String WIDGET_COLOR = "widget_color";
private static final int JOB_ID = -17001;
public static void updateWidget(Context context) {
@ -196,9 +193,11 @@ public class PlayerWidgetJobService extends SafeJobIntentService {
} else {
views.setViewVisibility(R.id.layout_center, View.VISIBLE);
}
SharedPreferences prefs = getSharedPreferences(WIDGET_PREFS, Context.MODE_PRIVATE);
int backgroundColor = prefs.getInt(WIDGET_COLOR + id, 0x262C31);
views.setInt(R.id.widgetLayout,"setBackgroundColor", backgroundColor);
SharedPreferences prefs = getSharedPreferences(PlayerWidget.PREFS_NAME, Context.MODE_PRIVATE);
int backgroundColor = prefs.getInt(PlayerWidget.KEY_WIDGET_COLOR + id, PlayerWidget.DEFAULT_COLOR);
views.setInt(R.id.widgetLayout, "setBackgroundColor", backgroundColor);
manager.updateAppWidget(id, views);
}
} else {

View File

@ -754,5 +754,5 @@
<!-- Widget settings -->
<string name="widget_create_button">Create widget</string>
<string name="widget_opacity">Widget opacity</string>
<string name="widget_opacity">Opacity</string>
</resources>