Initial widget commit?

This commit is contained in:
Jaculabilis 2016-10-15 13:32:30 -05:00
parent 42231f1248
commit a82677edc4
11 changed files with 153 additions and 59 deletions

View File

@ -8,15 +8,7 @@
android:label="@string/app_name" android:label="@string/app_name"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity android:name=".MainActivity"> <receiver android:name=".FrameWidget">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="layout.FrameWidget">
<intent-filter> <intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter> </intent-filter>
@ -25,6 +17,12 @@
android:name="android.appwidget.provider" android:name="android.appwidget.provider"
android:resource="@xml/frame_widget_info" /> android:resource="@xml/frame_widget_info" />
</receiver> </receiver>
<activity android:name=".FrameWidgetConfigureActivity">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
</application> </application>
</manifest> </manifest>

View File

@ -1,24 +1,23 @@
package layout; package horse.jaeil.microframe;
import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider; import android.appwidget.AppWidgetProvider;
import android.content.Context; import android.content.Context;
import android.widget.RemoteViews; import android.widget.RemoteViews;
import horse.jaeil.microframe.R;
/** /**
* Implementation of App Widget functionality. * Implementation of App Widget functionality.
* App Widget Configuration implemented in {@link FrameWidgetConfigureActivity FrameWidgetConfigureActivity}
*/ */
public class FrameWidget extends AppWidgetProvider { public class FrameWidget extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) { int appWidgetId) {
CharSequence widgetText = context.getString(R.string.appwidget_text); CharSequence widgetText = FrameWidgetConfigureActivity.loadTitlePref(context, appWidgetId);
// Construct the RemoteViews object // Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.frame_widget); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.frame_widget);
// views.setTextViewText(R.id.appwidget_text, widgetText); views.setTextViewText(R.id.appwidget_text, widgetText);
// Instruct the widget manager to update the widget // Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views); appWidgetManager.updateAppWidget(appWidgetId, views);
@ -32,6 +31,14 @@ public class FrameWidget extends AppWidgetProvider {
} }
} }
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// When the user deletes the widget, delete the preference associated with it.
for (int appWidgetId : appWidgetIds) {
FrameWidgetConfigureActivity.deleteTitlePref(context, appWidgetId);
}
}
@Override @Override
public void onEnabled(Context context) { public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created // Enter relevant functionality for when the first widget is created

View File

@ -0,0 +1,99 @@
package horse.jaeil.microframe;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
/**
* The configuration screen for the {@link FrameWidget FrameWidget} AppWidget.
*/
public class FrameWidgetConfigureActivity extends Activity {
private static final String PREFS_NAME = "horse.jaeil.microframe.FrameWidget";
private static final String PREF_PREFIX_KEY = "appwidget_";
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
EditText mAppWidgetText;
View.OnClickListener mOnClickListener = new View.OnClickListener() {
public void onClick(View v) {
final Context context = FrameWidgetConfigureActivity.this;
// When the button is clicked, store the string locally
String widgetText = mAppWidgetText.getText().toString();
saveTitlePref(context, mAppWidgetId, widgetText);
// It is the responsibility of the configuration activity to update the app widget
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
FrameWidget.updateAppWidget(context, appWidgetManager, mAppWidgetId);
// Make sure we pass back the original appWidgetId
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
};
public FrameWidgetConfigureActivity() {
super();
}
// Write the prefix to the SharedPreferences object for this widget
static void saveTitlePref(Context context, int appWidgetId, String text) {
SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
prefs.putString(PREF_PREFIX_KEY + appWidgetId, text);
prefs.apply();
}
// Read the prefix from the SharedPreferences object for this widget.
// If there is no preference saved, get the default from a resource
static String loadTitlePref(Context context, int appWidgetId) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
String titleValue = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null);
if (titleValue != null) {
return titleValue;
} else {
return context.getString(R.string.appwidget_text);
}
}
static void deleteTitlePref(Context context, int appWidgetId) {
SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
prefs.remove(PREF_PREFIX_KEY + appWidgetId);
prefs.apply();
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// Set the result to CANCELED. This will cause the widget host to cancel
// out of the widget placement if the user presses the back button.
setResult(RESULT_CANCELED);
setContentView(R.layout.frame_widget_configure);
mAppWidgetText = (EditText) findViewById(R.id.appwidget_text);
findViewById(R.id.add_button).setOnClickListener(mOnClickListener);
// Find the widget id from the intent.
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
// If this activity was started with an intent without an app widget ID, finish with an error.
if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
return;
}
mAppWidgetText.setText(loadTitlePref(FrameWidgetConfigureActivity.this, mAppWidgetId));
}
}

View File

@ -1,13 +0,0 @@
package horse.jaeil.microframe;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

View File

@ -1,18 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="horse.jaeil.microframe.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/about_text" />
</RelativeLayout>

View File

@ -14,7 +14,7 @@
android:background="#09C" android:background="#09C"
android:contentDescription="@string/appwidget_text" android:contentDescription="@string/appwidget_text"
android:text="@string/appwidget_text" android:text="@string/appwidget_text"
android:textColor="#ff0000" android:textColor="#ffffff"
android:textSize="24sp" android:textSize="24sp"
android:textStyle="bold|italic" /> android:textStyle="bold|italic" />

View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="@string/configure" />
<EditText
android:id="@+id/appwidget_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
<Button
android:id="@+id/add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/add_widget" />
</LinearLayout>

View File

@ -1,6 +0,0 @@
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
</resources>

View File

@ -1,11 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<!-- <!--
Refer to App Widget Documentation for margin information Refer to App Widget Documentation for margin information
http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
--> -->
<dimen name="widget_margin">8dp</dimen> <dimen name="widget_margin">8dp</dimen>
</resources>
</resources>

View File

@ -1,6 +1,6 @@
<resources> <resources>
<string name="app_name">MicroFrame</string> <string name="app_name">MicroFrame</string>
<string name="about_text">Version: 0.1.1\nAuthor: Tim Van Baak</string> <string name="appwidget_text">EXAMPLE</string>
<string name="appwidget_text">WAITAMINUTE</string> <string name="configure">Configure</string>
<string name="add_widget">Add widget????</string> <string name="add_widget">Add widget</string>
</resources> </resources>

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:configure="horse.jaeil.microframe.FrameWidgetConfigureActivity"
android:initialKeyguardLayout="@layout/frame_widget" android:initialKeyguardLayout="@layout/frame_widget"
android:initialLayout="@layout/frame_widget" android:initialLayout="@layout/frame_widget"
android:minHeight="40dp" android:minHeight="40dp"