Skip to content

Commit

Permalink
add widget
Browse files Browse the repository at this point in the history
update version
  • Loading branch information
the0cp committed Jul 30, 2024
1 parent 50e9698 commit 1f2bab4
Show file tree
Hide file tree
Showing 23 changed files with 237 additions and 220 deletions.
8 changes: 5 additions & 3 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
minSdk = 24
//noinspection OldTargetApi
targetSdk = 34
versionCode = 4
versionName = "1.3"
versionCode = 5
versionName = "1.4"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
Expand All @@ -34,10 +34,12 @@ android {
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
viewBinding = true
}
}

dependencies {

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
Expand Down
29 changes: 18 additions & 11 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.VIBRATE" />

<application
android:allowBackup="true"
Expand All @@ -16,18 +16,28 @@
android:supportsRtl="true"
android:theme="@style/Base.Theme.Objection"
tools:targetApi="31">
<receiver
android:name=".AppWidget"
android:exported="false">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/app_widget_info" />
<meta-data
android:name="android:previewImage"
android:resource="@drawable/objection" />
</receiver>

<activity
android:name=".SettingsActivity"
android:theme="@style/Theme.Settings"
android:exported="false"
android:label="@string/title_activity_settings"
android:screenOrientation="sensorLandscape"
tools:ignore="DiscouragedApi">


</activity>


android:theme="@style/Theme.Settings"
tools:ignore="DiscouragedApi" />
<activity
android:name=".MainActivity"
android:exported="true"
Expand All @@ -38,11 +48,8 @@
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>

</application>

</manifest>
59 changes: 59 additions & 0 deletions app/src/main/java/com/the0cp/objection/AppWidget.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.the0cp.objection

import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.Context
import android.content.Intent
import android.widget.RemoteViews

/**
* Implementation of App Widget functionality.
*/
class AppWidget : AppWidgetProvider() {
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
// There may be multiple widgets active, so update all of them
for (appWidgetId in appWidgetIds) {
val views = RemoteViews(context.packageName, R.layout.app_widget)
views.setImageViewResource(R.id.imageView, R.drawable.objection)

val intent = Intent(context, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)

views.setOnClickPendingIntent(R.id.widget_root, pendingIntent)
updateAppWidget(context, appWidgetManager, appWidgetId)
}
}

override fun onEnabled(context: Context) {
// Enter relevant functionality for when the first widget is created
}

override fun onDisabled(context: Context) {
// Enter relevant functionality for when the last widget is disabled
}
}

internal fun updateAppWidget(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetId: Int
) {
// val widgetText = context.getString(R.string.appwidget_text)
// Construct the RemoteViews object
val views = RemoteViews(context.packageName, R.layout.app_widget)
// views.setTextViewText(R.id.appwidget_text, widgetText)

views.setImageViewResource(R.id.imageView, R.drawable.objection)
// Instruct the widget manager to update the widget

val intent = Intent(context, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)

views.setOnClickPendingIntent(R.id.widget_root, pendingIntent)
appWidgetManager.updateAppWidget(appWidgetId, views)
}
10 changes: 10 additions & 0 deletions app/src/main/res/drawable-v21/app_widget_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?><!--
Background for widgets to make the rounded corners based on the
appWidgetRadius attribute value
-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<corners android:radius="?attr/appWidgetRadius" />
<solid android:color="?android:attr/colorBackground" />
</shape>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable-v21/app_widget_inner_view_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?><!--
Background for views inside widgets to make the rounded corners based on the
appWidgetInnerRadius attribute value
-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<corners android:radius="?attr/appWidgetInnerRadius" />
<solid android:color="?android:attr/colorAccent" />
</shape>
170 changes: 0 additions & 170 deletions app/src/main/res/drawable/ic_launcher_background.xml

This file was deleted.

30 changes: 0 additions & 30 deletions app/src/main/res/drawable/ic_launcher_foreground.xml

This file was deleted.

4 changes: 0 additions & 4 deletions app/src/main/res/drawable/ic_visibility_off.xml

This file was deleted.

Binary file added app/src/main/res/drawable/objection.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions app/src/main/res/layout/app_widget.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
style="@style/Widget.Objection.AppWidget.Container"
android:id="@+id/widget_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/Base.Theme.Objection.AppWidgetContainer">

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
app:srcCompat="@drawable/objection"
tools:srcCompat="@drawable/objection" />
</RelativeLayout>
Loading

0 comments on commit 1f2bab4

Please sign in to comment.