WallpaperComplication -> kt

This commit is contained in:
Milos Kozak 2022-05-02 22:42:18 +02:00
parent 60ea0421c8
commit 89f86df482
8 changed files with 80 additions and 123 deletions

View file

@ -1,57 +0,0 @@
package info.nightscout.androidaps.complications;
import android.app.PendingIntent;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Icon;
import android.support.wearable.complications.ComplicationData;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import java.io.IOException;
import java.io.InputStream;
import info.nightscout.androidaps.data.RawDisplayData;
import info.nightscout.shared.logging.LTag;
/*
* Created by dlvoy on 2019-11-12
*/
@SuppressWarnings("deprecation")
public abstract class WallpaperComplication extends BaseComplicationProviderService {
public abstract String getWallpaperAssetsFileName();
public ComplicationData buildComplicationData(int dataType, RawDisplayData raw, PendingIntent complicationPendingIntent) {
ComplicationData complicationData = null;
if (dataType == ComplicationData.TYPE_LARGE_IMAGE) {
DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
final ComplicationData.Builder builder = new ComplicationData.Builder(ComplicationData.TYPE_LARGE_IMAGE);
AssetManager assetManager = getAssets();
try (InputStream iStr = assetManager.open(getWallpaperAssetsFileName())) {
Bitmap bitmap = BitmapFactory.decodeStream(iStr);
Bitmap scaled = Bitmap.createScaledBitmap(bitmap, width, height, true);
builder.setLargeImage(Icon.createWithBitmap(scaled));
} catch (IOException e) {
aapsLogger.error(LTag.WEAR, "Cannot read wallpaper asset: " + e.getMessage(), e);
}
complicationData = builder.build();
} else {
aapsLogger.warn(LTag.WEAR, "Unexpected complication type " + dataType);
}
return complicationData;
}
}

View file

@ -0,0 +1,47 @@
@file:Suppress("DEPRECATION")
package info.nightscout.androidaps.complications
import android.app.PendingIntent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.Icon
import android.support.wearable.complications.ComplicationData
import android.util.DisplayMetrics
import android.view.WindowManager
import info.nightscout.androidaps.data.RawDisplayData
import info.nightscout.shared.logging.LTag
import java.io.IOException
/*
* Created by dlvoy on 2019-11-12
*/
abstract class WallpaperComplication : BaseComplicationProviderService() {
abstract val wallpaperAssetsFileName: String
override fun buildComplicationData(dataType: Int, raw: RawDisplayData, complicationPendingIntent: PendingIntent): ComplicationData? {
var complicationData: ComplicationData? = null
if (dataType == ComplicationData.TYPE_LARGE_IMAGE) {
val metrics = DisplayMetrics()
val windowManager = applicationContext.getSystemService(WINDOW_SERVICE) as WindowManager
windowManager.defaultDisplay.getMetrics(metrics)
val width = metrics.widthPixels
val height = metrics.heightPixels
val builder = ComplicationData.Builder(ComplicationData.TYPE_LARGE_IMAGE)
val assetManager = assets
try {
assetManager.open(wallpaperAssetsFileName).use { iStr ->
val bitmap = BitmapFactory.decodeStream(iStr)
val scaled = Bitmap.createScaledBitmap(bitmap, width, height, true)
builder.setLargeImage(Icon.createWithBitmap(scaled))
}
} catch (e: IOException) {
aapsLogger.error(LTag.WEAR, "Cannot read wallpaper asset: " + e.message, e)
}
complicationData = builder.build()
} else {
aapsLogger.warn(LTag.WEAR, "Unexpected complication type $dataType")
}
return complicationData
}
}

View file

@ -1,22 +0,0 @@
package info.nightscout.androidaps.complications;
/*
* Created by dlvoy on 2019-11-12
*/
public class WallpaperDarkComplication extends WallpaperComplication {
@Override
public String getWallpaperAssetsFileName() {
return "watch_dark.jpg";
}
@Override
public String getProviderCanonicalName() {
return WallpaperDarkComplication.class.getCanonicalName();
}
@Override
public ComplicationAction getComplicationAction() {
return ComplicationAction.NONE;
}
}

View file

@ -0,0 +1,11 @@
package info.nightscout.androidaps.complications
/*
* Created by dlvoy on 2019-11-12
*/
class WallpaperDarkComplication : WallpaperComplication() {
override val wallpaperAssetsFileName: String = "watch_dark.jpg"
override fun getProviderCanonicalName(): String = WallpaperDarkComplication::class.java.canonicalName!!
override fun getComplicationAction(): ComplicationAction = ComplicationAction.NONE
}

View file

@ -1,22 +0,0 @@
package info.nightscout.androidaps.complications;
/*
* Created by dlvoy on 2019-11-12
*/
public class WallpaperGrayComplication extends WallpaperComplication {
@Override
public String getWallpaperAssetsFileName() {
return "watch_gray.jpg";
}
@Override
public String getProviderCanonicalName() {
return WallpaperGrayComplication.class.getCanonicalName();
}
@Override
public ComplicationAction getComplicationAction() {
return ComplicationAction.NONE;
}
}

View file

@ -0,0 +1,11 @@
package info.nightscout.androidaps.complications
/*
* Created by dlvoy on 2019-11-12
*/
class WallpaperGrayComplication : WallpaperComplication() {
override val wallpaperAssetsFileName: String = "watch_gray.jpg"
override fun getProviderCanonicalName(): String = WallpaperGrayComplication::class.java.canonicalName!!
override fun getComplicationAction(): ComplicationAction = ComplicationAction.NONE
}

View file

@ -1,22 +0,0 @@
package info.nightscout.androidaps.complications;
/*
* Created by dlvoy on 2019-11-12
*/
public class WallpaperLightComplication extends WallpaperComplication {
@Override
public String getWallpaperAssetsFileName() {
return "watch_light.jpg";
}
@Override
public String getProviderCanonicalName() {
return WallpaperLightComplication.class.getCanonicalName();
}
@Override
public ComplicationAction getComplicationAction() {
return ComplicationAction.NONE;
}
}

View file

@ -0,0 +1,11 @@
package info.nightscout.androidaps.complications
/*
* Created by dlvoy on 2019-11-12
*/
class WallpaperLightComplication : WallpaperComplication() {
override val wallpaperAssetsFileName: String = "watch_light.jpg"
override fun getProviderCanonicalName(): String = WallpaperLightComplication::class.java.canonicalName!!
override fun getComplicationAction(): ComplicationAction = ComplicationAction.NONE
}