Skip to content

Commit

Permalink
Added first steps towards configurable quick capture widget.
Browse files Browse the repository at this point in the history
  • Loading branch information
hdweiss committed Dec 10, 2012
1 parent 579dfea commit 39e4bba
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 7 deletions.
18 changes: 17 additions & 1 deletion AndroidManifest.xml
Expand Up @@ -24,7 +24,7 @@
android:value=".Gui.SearchActivity" />

<receiver
android:name=".Services.MobileOrgWidget"
android:name=".Gui.Widget.MobileOrgWidget"
android:label="MobileOrg Tasks" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
Expand Down Expand Up @@ -193,6 +193,22 @@
</activity>
<activity android:name=".Gui.Agenda.AgendaActivity" >
</activity>

<receiver android:name=".Gui.Widget.CaptureWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/capture_widget_provider" />
</receiver>

<activity android:name=".Gui.Widget.CaptureWidgetConfig" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
</application>

</manifest>
Binary file added res/drawable-hdpi/widget_capture.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions res/drawable/capture_widget_text_bg.xml
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/background_dark"/>

<padding
android:left="5dp"
android:top="1dp"
android:right="5dp"
android:bottom="1dp"/>

<corners
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
</shape>
26 changes: 26 additions & 0 deletions res/layout/capture_widget.xml
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/capture_widget_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:orientation="vertical" >

<ImageView
android:id="@+id/capture_widget_icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:src="@drawable/widget_capture" />

<TextView
android:id="@+id/capture_widget_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/capture_widget_text_bg"
android:text="Widget"
android:textColor="@android:color/primary_text_dark" />

</LinearLayout>
38 changes: 38 additions & 0 deletions res/layout/capture_widget_config.xml
@@ -0,0 +1,38 @@
<?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="match_parent"
android:orientation="vertical" >

<LinearLayout
style="@style/EditBorders"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="3dip" >

<fragment
android:name="com.matburt.mobileorg.Gui.Capture.LocationFragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:tag="captureLocationFragment" />
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Button
android:id="@+id/widget_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save" />

<Button
android:id="@+id/widget_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancel" />
</LinearLayout>

</LinearLayout>
10 changes: 10 additions & 0 deletions res/xml/capture_widget_provider.xml
@@ -0,0 +1,10 @@
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:configure="com.matburt.mobileorg.Gui.Widget.CaptureWidgetConfig"
android:initialLayout="@layout/capture_widget"
android:minHeight="72dp"
android:minWidth="72dp"
android:previewImage="@drawable/icon"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="86400000" >

</appwidget-provider>
17 changes: 12 additions & 5 deletions src/com/matburt/mobileorg/Gui/Capture/LocationFragment.java
Expand Up @@ -5,6 +5,7 @@

import android.content.ContentResolver;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -41,17 +42,23 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

EditActivity activity = (EditActivity) getActivity();
FragmentActivity activity = getActivity();
this.resolver = activity.getContentResolver();

restoreFromBundle(savedInstanceState);

if(this.node == null)
this.node = activity.getParentOrgNode();
boolean isModifiable = true;
try {
EditActivity editActivity = (EditActivity) activity;

initLocationView();
if (this.node == null)
this.node = editActivity.getParentOrgNode();

isModifiable = editActivity.isNodeRefilable();
} catch (ClassCastException e) {}

setModifiable(activity.isNodeRefilable());
initLocationView();
setModifiable(isModifiable);
}

@Override
Expand Down
77 changes: 77 additions & 0 deletions src/com/matburt/mobileorg/Gui/Widget/CaptureWidgetConfig.java
@@ -0,0 +1,77 @@
package com.matburt.mobileorg.Gui.Widget;

import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.matburt.mobileorg.R;
import com.matburt.mobileorg.Gui.Capture.LocationFragment;
import com.matburt.mobileorg.OrgData.OrgNode;

public class CaptureWidgetConfig extends SherlockFragmentActivity {

private int mAppWidgetId;
private LocationFragment locationFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.capture_widget_config);

Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}

((Button) findViewById(R.id.widget_save)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
saveConfig();
}
});

((Button) findViewById(R.id.widget_cancel)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
cancelConfig();
}
});

locationFragment = (LocationFragment) getSupportFragmentManager()
.findFragmentByTag("captureLocationFragment");
}


private void saveConfig() {
save();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
CaptureWidgetProvider.updateWidget(mAppWidgetId, appWidgetManager, this);

Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}

private void save() {
OrgNode node = locationFragment.getLocationSelection();
String locationOlpId = node.getOlpId(getContentResolver());

CaptureWidgetProvider.writeConfig(mAppWidgetId, locationOlpId,
getApplicationContext());
}

private void cancelConfig() {
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_CANCELED, resultValue);
finish();
}
}
70 changes: 70 additions & 0 deletions src/com/matburt/mobileorg/Gui/Widget/CaptureWidgetProvider.java
@@ -0,0 +1,70 @@
package com.matburt.mobileorg.Gui.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.content.SharedPreferences.Editor;
import android.widget.RemoteViews;

import com.matburt.mobileorg.R;
import com.matburt.mobileorg.Gui.Capture.EditActivity;

public class CaptureWidgetProvider extends AppWidgetProvider {
public static final String LOCATION = "location";

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
try {
for (int i = 0; i < appWidgetIds.length; i++) {
int appWidgetId = appWidgetIds[i];
updateWidget(appWidgetId, appWidgetManager, context);
}
} catch (Exception e) {
e.printStackTrace();
}
}

public static void updateWidget(int appWidgetId,
AppWidgetManager appWidgetManager, Context context) {
SharedPreferences prefs = context.getSharedPreferences("widget_"
+ appWidgetId, Context.MODE_PRIVATE);
if (null == prefs)
return;

RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.capture_widget);

String location = prefs.getString(LOCATION, "???");
views.setTextViewText(R.id.capture_widget_text,
prefs.getString("name", location));

Intent intent = getWidgetIntent(appWidgetId, context);

PendingIntent pendingIntent = PendingIntent.getActivity(
context, appWidgetId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
views.setOnClickPendingIntent(R.id.capture_widget_root,
pendingIntent);

appWidgetManager.updateAppWidget(appWidgetId, views);
}

private static Intent getWidgetIntent(int appWidgetId, Context context) {
Intent intent = new Intent(context, EditActivity.class);

return intent;
}

public static void writeConfig(int appWidgetId, String locationOlp, Context context) {
SharedPreferences prefs = context.getSharedPreferences("widget_"
+ appWidgetId, Context.MODE_PRIVATE);

Editor edit = prefs.edit();
edit.putString(LOCATION, locationOlp);
edit.commit();
}
}
@@ -1,4 +1,4 @@
package com.matburt.mobileorg.Services;
package com.matburt.mobileorg.Gui.Widget;

import android.app.PendingIntent;
import android.app.Service;
Expand Down

0 comments on commit 39e4bba

Please sign in to comment.