move MyWidgetProvider to kotlin
This commit is contained in:
parent
25634e428d
commit
9676c5981f
6 changed files with 223 additions and 241 deletions
|
@ -67,7 +67,7 @@
|
|||
</service>
|
||||
|
||||
<receiver
|
||||
android:name=".MyWidgetProvider"
|
||||
android:name=".helpers.MyWidgetProvider"
|
||||
android:icon="@mipmap/widget_preview">
|
||||
<intent-filter>
|
||||
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
|
||||
|
|
|
@ -1,238 +0,0 @@
|
|||
package com.simplemobiletools.musicplayer;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.appwidget.AppWidgetProvider;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Color;
|
||||
import android.os.Bundle;
|
||||
import android.widget.RemoteViews;
|
||||
|
||||
import com.simplemobiletools.musicplayer.activities.MainActivity;
|
||||
import com.simplemobiletools.musicplayer.helpers.BusProvider;
|
||||
import com.simplemobiletools.musicplayer.models.Events;
|
||||
import com.simplemobiletools.musicplayer.models.Song;
|
||||
import com.squareup.otto.Bus;
|
||||
import com.squareup.otto.Subscribe;
|
||||
|
||||
public class MyWidgetProvider extends AppWidgetProvider {
|
||||
private static RemoteViews mRemoteViews;
|
||||
private static AppWidgetManager mWidgetManager;
|
||||
private static Context mContext;
|
||||
private static Intent mIntent;
|
||||
private static Bus mBus;
|
||||
private static Song mCurrSong;
|
||||
private static Bitmap mPlayBitmap;
|
||||
private static Bitmap mPauseBitmap;
|
||||
|
||||
private static boolean mIsPlaying;
|
||||
private static int[] mWidgetIds;
|
||||
|
||||
@Override
|
||||
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||
initVariables(context);
|
||||
super.onUpdate(context, appWidgetManager, appWidgetIds);
|
||||
}
|
||||
|
||||
private void setupIntent(String action, int id) {
|
||||
mIntent.setAction(action);
|
||||
final PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, mIntent, 0);
|
||||
|
||||
if (mRemoteViews != null)
|
||||
mRemoteViews.setOnClickPendingIntent(id, pendingIntent);
|
||||
}
|
||||
|
||||
private void setupAppOpenIntent(int id) {
|
||||
final Intent intent = new Intent(mContext, MainActivity.class);
|
||||
final PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
|
||||
mRemoteViews.setOnClickPendingIntent(id, pendingIntent);
|
||||
}
|
||||
|
||||
private void initVariables(Context context) {
|
||||
mContext = context;
|
||||
mIntent = new Intent(mContext, MyWidgetProvider.class);
|
||||
updateWidgetIds();
|
||||
for (int widgetId : mWidgetIds) {
|
||||
mRemoteViews = getRemoteViews(mWidgetManager, mContext, widgetId);
|
||||
}
|
||||
|
||||
setupViews(mContext);
|
||||
|
||||
if (mBus == null) {
|
||||
mBus = BusProvider.Companion.getInstance();
|
||||
}
|
||||
registerBus();
|
||||
}
|
||||
|
||||
private void updateWidgetIds() {
|
||||
final ComponentName component = new ComponentName(mContext, MyWidgetProvider.class);
|
||||
mWidgetManager = AppWidgetManager.getInstance(mContext);
|
||||
mWidgetIds = mWidgetManager.getAppWidgetIds(component);
|
||||
}
|
||||
|
||||
private SharedPreferences initPrefs(Context context) {
|
||||
return context.getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void songChangedEvent(Events.SongChanged event) {
|
||||
mCurrSong = event.getSong();
|
||||
updateSongInfo();
|
||||
updateWidgets();
|
||||
}
|
||||
|
||||
private void updateSongInfo() {
|
||||
String title = "";
|
||||
String artist = "";
|
||||
if (mCurrSong != null) {
|
||||
title = mCurrSong.getTitle();
|
||||
artist = mCurrSong.getArtist();
|
||||
}
|
||||
|
||||
if (mRemoteViews == null)
|
||||
return;
|
||||
|
||||
mRemoteViews.setTextViewText(R.id.songTitle, title);
|
||||
mRemoteViews.setTextViewText(R.id.songArtist, artist);
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void songStateChanged(Events.SongStateChanged event) {
|
||||
if (mIsPlaying == event.isPlaying())
|
||||
return;
|
||||
|
||||
mIsPlaying = event.isPlaying();
|
||||
updatePlayPauseButton();
|
||||
updateWidgets();
|
||||
}
|
||||
|
||||
private void updatePlayPauseButton() {
|
||||
Bitmap bmp = mPlayBitmap;
|
||||
|
||||
if (mIsPlaying)
|
||||
bmp = mPauseBitmap;
|
||||
|
||||
mRemoteViews.setImageViewBitmap(R.id.playPauseBtn, bmp);
|
||||
}
|
||||
|
||||
private void updateWidgets() {
|
||||
for (int widgetId : mWidgetIds) {
|
||||
mWidgetManager.updateAppWidget(widgetId, mRemoteViews);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateColors(Context context) {
|
||||
final SharedPreferences prefs = initPrefs(context);
|
||||
final Resources res = context.getResources();
|
||||
final int defaultColor = res.getColor(R.color.dark_grey_transparent);
|
||||
final int newBgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, defaultColor);
|
||||
final int newTextColor = prefs.getInt(Constants.WIDGET_TEXT_COLOR, Color.WHITE);
|
||||
|
||||
if (mRemoteViews == null)
|
||||
initVariables(context);
|
||||
|
||||
mRemoteViews.setInt(R.id.widget_holder, "setBackgroundColor", newBgColor);
|
||||
|
||||
mRemoteViews.setInt(R.id.songTitle, "setTextColor", newTextColor);
|
||||
mRemoteViews.setInt(R.id.songArtist, "setTextColor", newTextColor);
|
||||
|
||||
Bitmap bmp = Utils.getColoredIcon(res, newTextColor, R.mipmap.previous);
|
||||
mRemoteViews.setImageViewBitmap(R.id.previousBtn, bmp);
|
||||
|
||||
mPlayBitmap = Utils.getColoredIcon(res, newTextColor, R.mipmap.play);
|
||||
mPauseBitmap = Utils.getColoredIcon(res, newTextColor, R.mipmap.pause);
|
||||
|
||||
bmp = Utils.getColoredIcon(res, newTextColor, R.mipmap.next);
|
||||
mRemoteViews.setImageViewBitmap(R.id.nextBtn, bmp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (mRemoteViews == null || mWidgetManager == null || mBus == null || mContext == null) {
|
||||
initVariables(context);
|
||||
}
|
||||
|
||||
final String action = intent.getAction();
|
||||
switch (action) {
|
||||
case Constants.PREVIOUS:
|
||||
case Constants.PLAYPAUSE:
|
||||
case Constants.NEXT:
|
||||
Utils.sendIntent(context, action);
|
||||
break;
|
||||
default:
|
||||
super.onReceive(context, intent);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDeleted(Context context, int[] appWidgetIds) {
|
||||
super.onDeleted(context, appWidgetIds);
|
||||
unregisterBus();
|
||||
updateWidgetIds();
|
||||
}
|
||||
|
||||
private void setupButtons() {
|
||||
setupIntent(Constants.PREVIOUS, R.id.previousBtn);
|
||||
setupIntent(Constants.PLAYPAUSE, R.id.playPauseBtn);
|
||||
setupIntent(Constants.NEXT, R.id.nextBtn);
|
||||
|
||||
setupAppOpenIntent(R.id.songTitle);
|
||||
setupAppOpenIntent(R.id.songArtist);
|
||||
}
|
||||
|
||||
private void setupViews(Context context) {
|
||||
updateWidgetIds();
|
||||
updateColors(context);
|
||||
setupButtons();
|
||||
updateSongInfo();
|
||||
updatePlayPauseButton();
|
||||
updateWidgets();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int widgetId, Bundle newOptions) {
|
||||
mRemoteViews = getRemoteViews(appWidgetManager, context, widgetId);
|
||||
mWidgetManager = appWidgetManager;
|
||||
setupViews(context);
|
||||
super.onAppWidgetOptionsChanged(context, appWidgetManager, widgetId, newOptions);
|
||||
}
|
||||
|
||||
private RemoteViews getRemoteViews(AppWidgetManager appWidgetManager, Context context, int widgetId) {
|
||||
final Bundle options = appWidgetManager.getAppWidgetOptions(widgetId);
|
||||
final int minHeight = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
|
||||
final int rows = getCellsForSize(minHeight);
|
||||
|
||||
int layoutId = R.layout.widget;
|
||||
if (rows == 1)
|
||||
layoutId = R.layout.small_widget;
|
||||
|
||||
return new RemoteViews(context.getPackageName(), layoutId);
|
||||
}
|
||||
|
||||
private static int getCellsForSize(int size) {
|
||||
int n = 2;
|
||||
while (70 * n - 30 < size) {
|
||||
++n;
|
||||
}
|
||||
return n - 1;
|
||||
}
|
||||
|
||||
private void registerBus() {
|
||||
try {
|
||||
mBus.register(this);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
private void unregisterBus() {
|
||||
try {
|
||||
mBus.unregister(this);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
|
@ -16,7 +16,7 @@ import android.widget.SeekBar;
|
|||
import android.widget.TextView;
|
||||
|
||||
import com.simplemobiletools.musicplayer.Constants;
|
||||
import com.simplemobiletools.musicplayer.MyWidgetProvider;
|
||||
import com.simplemobiletools.musicplayer.helpers.MyWidgetProvider;
|
||||
import com.simplemobiletools.musicplayer.R;
|
||||
|
||||
import butterknife.BindView;
|
||||
|
|
|
@ -2,6 +2,7 @@ package com.simplemobiletools.musicplayer.extensions
|
|||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import com.simplemobiletools.musicplayer.Constants
|
||||
import com.simplemobiletools.musicplayer.MusicService
|
||||
|
||||
fun Context.sendIntent(action: String) {
|
||||
|
@ -10,3 +11,5 @@ fun Context.sendIntent(action: String) {
|
|||
startService(this)
|
||||
}
|
||||
}
|
||||
|
||||
fun Context.getSharedPrefs() = getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE)
|
||||
|
|
|
@ -3,6 +3,7 @@ package com.simplemobiletools.musicplayer.helpers
|
|||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import com.simplemobiletools.musicplayer.Constants
|
||||
import com.simplemobiletools.musicplayer.extensions.getSharedPrefs
|
||||
|
||||
class Config private constructor(context: Context) {
|
||||
private val mPrefs: SharedPreferences
|
||||
|
@ -14,7 +15,7 @@ class Config private constructor(context: Context) {
|
|||
}
|
||||
|
||||
init {
|
||||
mPrefs = context.getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE)
|
||||
mPrefs = context.getSharedPrefs()
|
||||
}
|
||||
|
||||
var isFirstRun: Boolean
|
||||
|
|
|
@ -0,0 +1,216 @@
|
|||
package com.simplemobiletools.musicplayer.helpers
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.appwidget.AppWidgetManager
|
||||
import android.appwidget.AppWidgetProvider
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Color
|
||||
import android.os.Bundle
|
||||
import android.widget.RemoteViews
|
||||
import com.simplemobiletools.musicplayer.Constants
|
||||
import com.simplemobiletools.musicplayer.R
|
||||
import com.simplemobiletools.musicplayer.Utils
|
||||
import com.simplemobiletools.musicplayer.activities.MainActivity
|
||||
import com.simplemobiletools.musicplayer.extensions.getSharedPrefs
|
||||
import com.simplemobiletools.musicplayer.models.Events
|
||||
import com.simplemobiletools.musicplayer.models.Song
|
||||
import com.squareup.otto.Bus
|
||||
import com.squareup.otto.Subscribe
|
||||
|
||||
class MyWidgetProvider : AppWidgetProvider() {
|
||||
companion object {
|
||||
private var mBus: Bus? = null
|
||||
private var mCurrSong: Song? = null
|
||||
private var mPlayBitmap: Bitmap? = null
|
||||
private var mPauseBitmap: Bitmap? = null
|
||||
private var mIsPlaying = false
|
||||
private var mWidgetIds: IntArray? = null
|
||||
|
||||
lateinit var mRemoteViews: RemoteViews
|
||||
lateinit var mWidgetManager: AppWidgetManager
|
||||
lateinit var mContext: Context
|
||||
lateinit var mIntent: Intent
|
||||
|
||||
private fun getCellsForSize(size: Int): Int {
|
||||
var n = 2
|
||||
while (70 * n - 30 < size) {
|
||||
++n
|
||||
}
|
||||
return n - 1
|
||||
}
|
||||
}
|
||||
|
||||
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
|
||||
initVariables(context)
|
||||
super.onUpdate(context, appWidgetManager, appWidgetIds)
|
||||
}
|
||||
|
||||
private fun initVariables(context: Context) {
|
||||
mContext = context
|
||||
mIntent = Intent(mContext, MyWidgetProvider::class.java)
|
||||
mWidgetManager = AppWidgetManager.getInstance(mContext)
|
||||
updateWidgetIds()
|
||||
for (widgetId in mWidgetIds!!) {
|
||||
mRemoteViews = getRemoteViews(mWidgetManager, mContext, widgetId)
|
||||
}
|
||||
|
||||
setupViews(mContext)
|
||||
|
||||
if (mBus == null) {
|
||||
mBus = BusProvider.instance
|
||||
}
|
||||
registerBus()
|
||||
}
|
||||
|
||||
private fun setupIntent(action: String, id: Int) {
|
||||
mIntent.action = action
|
||||
val pendingIntent = PendingIntent.getBroadcast(mContext, 0, mIntent, 0)
|
||||
mRemoteViews.setOnClickPendingIntent(id, pendingIntent)
|
||||
}
|
||||
|
||||
private fun setupAppOpenIntent(id: Int) {
|
||||
val intent = Intent(mContext, MainActivity::class.java)
|
||||
val pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0)
|
||||
mRemoteViews.setOnClickPendingIntent(id, pendingIntent)
|
||||
}
|
||||
|
||||
private fun updateWidgetIds() {
|
||||
val component = ComponentName(mContext, MyWidgetProvider::class.java)
|
||||
mWidgetIds = mWidgetManager.getAppWidgetIds(component)
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
fun songChangedEvent(event: Events.SongChanged) {
|
||||
mCurrSong = event.song
|
||||
updateSongInfo()
|
||||
updateWidgets()
|
||||
}
|
||||
|
||||
private fun updateSongInfo() {
|
||||
var title = ""
|
||||
var artist = ""
|
||||
if (mCurrSong != null) {
|
||||
title = mCurrSong!!.title
|
||||
artist = mCurrSong!!.artist
|
||||
}
|
||||
|
||||
mRemoteViews.setTextViewText(R.id.songTitle, title)
|
||||
mRemoteViews.setTextViewText(R.id.songArtist, artist)
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
fun songStateChanged(event: Events.SongStateChanged) {
|
||||
if (mIsPlaying == event.isPlaying)
|
||||
return
|
||||
|
||||
mIsPlaying = event.isPlaying
|
||||
updatePlayPauseButton()
|
||||
updateWidgets()
|
||||
}
|
||||
|
||||
private fun updatePlayPauseButton() {
|
||||
val bmp = if (mIsPlaying) mPauseBitmap else mPlayBitmap
|
||||
mRemoteViews.setImageViewBitmap(R.id.playPauseBtn, bmp)
|
||||
}
|
||||
|
||||
private fun updateWidgets() {
|
||||
for (widgetId in mWidgetIds!!) {
|
||||
mWidgetManager.updateAppWidget(widgetId, mRemoteViews)
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateColors() {
|
||||
val prefs = mContext.getSharedPrefs()
|
||||
val res = mContext.resources
|
||||
val defaultColor = res.getColor(R.color.dark_grey_transparent)
|
||||
val newBgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, defaultColor)
|
||||
val newTextColor = prefs.getInt(Constants.WIDGET_TEXT_COLOR, Color.WHITE)
|
||||
var bmp = Utils.getColoredIcon(res, newTextColor, R.mipmap.previous)
|
||||
|
||||
mRemoteViews.apply {
|
||||
setInt(R.id.widget_holder, "setBackgroundColor", newBgColor)
|
||||
setInt(R.id.songTitle, "setTextColor", newTextColor)
|
||||
setInt(R.id.songArtist, "setTextColor", newTextColor)
|
||||
|
||||
setImageViewBitmap(R.id.previousBtn, bmp)
|
||||
|
||||
mPlayBitmap = Utils.getColoredIcon(res, newTextColor, R.mipmap.play)
|
||||
mPauseBitmap = Utils.getColoredIcon(res, newTextColor, R.mipmap.pause)
|
||||
|
||||
bmp = Utils.getColoredIcon(res, newTextColor, R.mipmap.next)
|
||||
setImageViewBitmap(R.id.nextBtn, bmp)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val action = intent.action
|
||||
when (action) {
|
||||
Constants.PREVIOUS, Constants.PLAYPAUSE, Constants.NEXT -> Utils.sendIntent(context, action)
|
||||
else -> super.onReceive(context, intent)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDeleted(context: Context, appWidgetIds: IntArray) {
|
||||
super.onDeleted(context, appWidgetIds)
|
||||
unregisterBus()
|
||||
mContext = context
|
||||
updateWidgetIds()
|
||||
}
|
||||
|
||||
private fun setupButtons() {
|
||||
setupIntent(Constants.PREVIOUS, R.id.previousBtn)
|
||||
setupIntent(Constants.PLAYPAUSE, R.id.playPauseBtn)
|
||||
setupIntent(Constants.NEXT, R.id.nextBtn)
|
||||
|
||||
setupAppOpenIntent(R.id.songTitle)
|
||||
setupAppOpenIntent(R.id.songArtist)
|
||||
}
|
||||
|
||||
private fun setupViews(context: Context) {
|
||||
mContext = context
|
||||
updateWidgetIds()
|
||||
updateColors()
|
||||
setupButtons()
|
||||
updateSongInfo()
|
||||
updatePlayPauseButton()
|
||||
updateWidgets()
|
||||
}
|
||||
|
||||
override fun onAppWidgetOptionsChanged(context: Context, appWidgetManager: AppWidgetManager, widgetId: Int, newOptions: Bundle) {
|
||||
mRemoteViews = getRemoteViews(appWidgetManager, context, widgetId)
|
||||
mWidgetManager = appWidgetManager
|
||||
setupViews(context)
|
||||
super.onAppWidgetOptionsChanged(context, appWidgetManager, widgetId, newOptions)
|
||||
}
|
||||
|
||||
private fun getRemoteViews(appWidgetManager: AppWidgetManager, context: Context, widgetId: Int): RemoteViews {
|
||||
val options = appWidgetManager.getAppWidgetOptions(widgetId)
|
||||
val minHeight = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT)
|
||||
val rows = getCellsForSize(minHeight)
|
||||
|
||||
var layoutId = R.layout.widget
|
||||
if (rows == 1)
|
||||
layoutId = R.layout.small_widget
|
||||
|
||||
return RemoteViews(context.packageName, layoutId)
|
||||
}
|
||||
|
||||
private fun registerBus() {
|
||||
try {
|
||||
mBus!!.register(this)
|
||||
} catch (e: Exception) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private fun unregisterBus() {
|
||||
try {
|
||||
mBus!!.unregister(this)
|
||||
} catch (e: Exception) {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue