Add android widget stub
This commit is contained in:
@@ -3,49 +3,67 @@
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
package="com.mowesta.android.app">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK"/>
|
||||
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
|
||||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
|
||||
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
|
||||
|
||||
<uses-sdk tools:overrideLibrary="androidx.security" />
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
||||
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||
|
||||
<application android:name=".MowestaApp"
|
||||
tools:targetApi="24"
|
||||
android:networkSecurityConfig="@xml/network"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:label="@string/application_long"
|
||||
android:theme="@style/Theme.Mowesta"
|
||||
android:allowBackup="true"
|
||||
android:supportsRtl="true"
|
||||
android:fullBackupContent="@xml/backup">
|
||||
<application
|
||||
android:name=".MowestaApp"
|
||||
android:allowBackup="true"
|
||||
android:fullBackupContent="@xml/backup"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/application_long"
|
||||
android:networkSecurityConfig="@xml/network"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/Theme.Mowesta"
|
||||
tools:targetApi="24">
|
||||
<activity
|
||||
android:launchMode="singleTask"
|
||||
android:name=".MowestaActivity"
|
||||
android:windowSoftInputMode="adjustResize"
|
||||
android:exported="true"
|
||||
android:label="@string/application_long">
|
||||
android:name=".MowestaActivity"
|
||||
android:exported="true"
|
||||
android:label="@string/application_long"
|
||||
android:launchMode="singleTask"
|
||||
android:windowSoftInputMode="adjustResize">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<receiver android:name=".MowestaReceiver">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.BOOT_COMPLETED"/>
|
||||
<action android:name="android.intent.action.BOOT_COMPLETED" />
|
||||
</intent-filter>
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
|
||||
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
|
||||
<service android:name=".MowestaService" />
|
||||
|
||||
<receiver android:name=".widget.ForecastWidgetProvider">
|
||||
<intent-filter>
|
||||
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
|
||||
</intent-filter>
|
||||
|
||||
<meta-data
|
||||
android:name="android.appwidget.provider"
|
||||
android:resource="@xml/forecast_widget_info" />
|
||||
</receiver>
|
||||
<activity android:name=".widget.ForecastWidgetConfig">
|
||||
<intent-filter>
|
||||
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
</manifest>
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.mowesta.android.app.widget;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.widget.RemoteViews;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.databinding.DataBindingUtil;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.mowesta.android.app.MowestaActivity;
|
||||
import com.mowesta.android.app.R;
|
||||
import com.mowesta.android.app.adapter.CityEntryPagedListAdapter;
|
||||
import com.mowesta.android.app.databinding.ActivityForecastWidgetConfigBinding;
|
||||
import com.mowesta.android.app.model.CitiesViewModel;
|
||||
import com.mowesta.android.feign.model.City;
|
||||
|
||||
public class ForecastWidgetConfig extends AppCompatActivity {
|
||||
|
||||
public static final String SHARED_PREFS = "widget_prefs";
|
||||
public static final String KEY_CITY_ID = "widget_city_ID";
|
||||
|
||||
private int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
|
||||
|
||||
private ActivityForecastWidgetConfigBinding binding;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
binding = DataBindingUtil.setContentView(this, R.layout.activity_forecast_widget_config);
|
||||
|
||||
|
||||
Intent configIntent = getIntent();
|
||||
Bundle extras = configIntent.getExtras();
|
||||
|
||||
if (extras != null)
|
||||
appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
|
||||
AppWidgetManager.INVALID_APPWIDGET_ID);
|
||||
|
||||
Intent resultValue = new Intent();
|
||||
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
|
||||
setResult(RESULT_CANCELED, resultValue);
|
||||
|
||||
if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
|
||||
finish();
|
||||
|
||||
RecyclerView recyclerView = binding.recycler;
|
||||
CityEntryPagedListAdapter adapter = new CityEntryPagedListAdapter(this, this::confirmConfiguration);
|
||||
recyclerView.setAdapter(adapter);
|
||||
|
||||
CitiesViewModel model = new ViewModelProvider(this).get(CitiesViewModel.class);
|
||||
|
||||
model.getCities().observe(this, adapter::submitList);
|
||||
|
||||
}
|
||||
|
||||
public void confirmConfiguration(City city) {
|
||||
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
|
||||
|
||||
Intent intent = new Intent(this, MowestaActivity.class);
|
||||
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
|
||||
|
||||
RemoteViews views = new RemoteViews(this.getPackageName(), R.layout.forecast_widget);
|
||||
views.setOnClickPendingIntent(R.id.button, pendingIntent);
|
||||
|
||||
views.setCharSequence(R.id.button, "setText", city.getWikipedia());
|
||||
|
||||
appWidgetManager.updateAppWidget(appWidgetId, views);
|
||||
|
||||
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
|
||||
SharedPreferences.Editor editor = prefs.edit();
|
||||
editor.putLong(KEY_CITY_ID + appWidgetId, city.getId());
|
||||
editor.apply();
|
||||
|
||||
Intent resultValue = new Intent();
|
||||
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
|
||||
setResult(RESULT_OK, resultValue);
|
||||
finish();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.mowesta.android.app.widget;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.appwidget.AppWidgetProvider;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.widget.RemoteViews;
|
||||
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import com.mowesta.android.app.MowestaActivity;
|
||||
import com.mowesta.android.app.R;
|
||||
import com.mowesta.android.app.model.ConditionViewModel;
|
||||
|
||||
import static com.mowesta.android.app.widget.ForecastWidgetConfig.KEY_CITY_ID;
|
||||
import static com.mowesta.android.app.widget.ForecastWidgetConfig.SHARED_PREFS;
|
||||
|
||||
public class ForecastWidgetProvider extends AppWidgetProvider {
|
||||
|
||||
@Override
|
||||
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||
|
||||
for (int widgetId : appWidgetIds) {
|
||||
Intent intent = new Intent(context, MowestaActivity.class);
|
||||
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
|
||||
|
||||
SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
|
||||
long cityId = prefs.getLong(KEY_CITY_ID + widgetId, -1L);
|
||||
|
||||
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.forecast_widget);
|
||||
views.setOnClickPendingIntent(R.id.button, pendingIntent);
|
||||
views.setCharSequence(R.id.button, "setText", "" + cityId);
|
||||
|
||||
appWidgetManager.updateAppWidget(widgetId, views);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<layout>
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout 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"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".widget.ForecastWidgetConfig">
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/recycler"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
|
||||
tools:listitem="@layout/view_city_entry" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</layout>
|
||||
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<ImageView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:srcCompat="@drawable/icon_launch_sun"
|
||||
android:layout_above="@id/button"
|
||||
android:layout_alignStart="@id/button"
|
||||
android:layout_alignEnd="@id/button"
|
||||
android:contentDescription="@string/action_customize" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/button"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_centerInParent="true"
|
||||
android:text="@string/action_customize" />
|
||||
|
||||
</RelativeLayout>
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:initialLayout="@layout/forecast_widget"
|
||||
android:minHeight="40dp"
|
||||
android:minWidth="110dp"
|
||||
android:minResizeWidth="40dp"
|
||||
android:resizeMode="horizontal|vertical"
|
||||
android:updatePeriodMillis="3600000"
|
||||
android:widgetCategory="home_screen"
|
||||
android:configure="com.mowesta.android.app.widget.ForecastWidgetConfig">
|
||||
|
||||
</appwidget-provider>
|
||||
Reference in New Issue
Block a user