Bladeren bron

Destinations in locale plugin

master
Chris Smith 14 jaren geleden
bovenliggende
commit
38f08283ff

+ 9
- 0
code/LocalePlugin/AndroidManifest.xml Bestand weergeven

@@ -19,16 +19,25 @@
19 19
             </intent-filter>
20 20
         </activity>
21 21
 
22
+        <activity android:name=".EditDestination" android:label="@string/location_name"
23
+            android:icon="@drawable/icon">
24
+            <intent-filter>
25
+                <action android:name="com.twofortyfouram.locale.intent.action.EDIT_CONDITION"/>
26
+            </intent-filter>
27
+        </activity>
28
+
22 29
          <receiver android:name=".Receiver" android:exported="true">
23 30
              <intent-filter>
24 31
                  <action android:name="com.twofortyfouram.locale.intent.action.QUERY_CONDITION" />
25 32
              </intent-filter>
26 33
              <intent-filter>
27 34
                  <action android:name="uk.co.md87.android.contextanalyser.ACTIVITY_CHANGED" />
35
+                 <action android:name="uk.co.md87.android.contextanalyser.CONTEXT_CHANGED" />
28 36
              </intent-filter>
29 37
          </receiver>
30 38
     </application>
31 39
 
32 40
     <uses-sdk android:minSdkVersion="3" />
33 41
     <uses-permission android:name="uk.co.md87.android.contextanalyser.RECEIVE_UPDATES"/>
42
+    <uses-permission android:name="uk.co.md87.android.contextanalyser.READ_PLACES"/>
34 43
 </manifest>

BIN
code/LocalePlugin/dist/LocalePlugin.apk Bestand weergeven


+ 1
- 0
code/LocalePlugin/res/values/strings.xml Bestand weergeven

@@ -2,4 +2,5 @@
2 2
 <resources>
3 3
     <string name="app_name">Locale Plugin</string>
4 4
     <string name="plugin_name">Activity</string>
5
+    <string name="location_name">Destination</string>
5 6
 </resources>

+ 1
- 1
code/LocalePlugin/src/uk/co/md87/android/contextanalyser/locale/EditActivity.java Bestand weergeven

@@ -39,7 +39,7 @@ import java.util.Arrays;
39 39
 import java.util.List;
40 40
 
41 41
 /**
42
- * This is the "Edit" activity for the locale plugin.
42
+ * Allows the user to edit their selected activity.
43 43
  *
44 44
  * @author chris
45 45
  */

+ 202
- 0
code/LocalePlugin/src/uk/co/md87/android/contextanalyser/locale/EditDestination.java Bestand weergeven

@@ -0,0 +1,202 @@
1
+/*
2
+ * Copyright (c) 2009-2010 Chris Smith
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package uk.co.md87.android.contextanalyser.locale;
24
+
25
+import android.app.Activity;
26
+import android.content.Context;
27
+import android.content.Intent;
28
+import android.content.pm.PackageManager;
29
+import android.database.Cursor;
30
+import android.net.Uri;
31
+import android.os.Bundle;
32
+import android.util.Log;
33
+import android.view.Menu;
34
+import android.view.MenuItem;
35
+import android.view.View;
36
+import android.view.ViewGroup;
37
+import android.widget.ArrayAdapter;
38
+import android.widget.CursorAdapter;
39
+import android.widget.LinearLayout;
40
+import android.widget.Spinner;
41
+import android.widget.TextView;
42
+
43
+import com.flurry.android.FlurryAgent;
44
+import com.twofortyfouram.SharedResources;
45
+
46
+import java.util.Arrays;
47
+import java.util.List;
48
+import uk.co.md87.android.contextapi.ContextApi;
49
+
50
+/**
51
+ * Allows the user to edit their selected destination.
52
+ *
53
+ * @author chris
54
+ */
55
+public class EditDestination extends Activity {
56
+
57
+    private static final int MENU_SAVE = 1;
58
+    private static final int MENU_DONT_SAVE = 2;
59
+
60
+    private boolean isCancelled;
61
+
62
+    @Override
63
+    public void onCreate(final Bundle savedInstanceState) {
64
+        super.onCreate(savedInstanceState);
65
+
66
+        FlurryAgent.onStartSession(this, "FRC4VR4CREJG3BTGSTLV");
67
+
68
+        setContentView(R.layout.main);
69
+
70
+        final String breadcrumbString = getIntent()
71
+                .getStringExtra(com.twofortyfouram.Intent.EXTRA_STRING_BREADCRUMB);
72
+        setTitle(String.format("%s%s%s", breadcrumbString,
73
+                com.twofortyfouram.Intent.BREADCRUMB_SEPARATOR,
74
+                getString(R.string.plugin_name)));
75
+       
76
+        ((LinearLayout) findViewById(R.id.frame))
77
+                .setBackgroundDrawable(SharedResources.getDrawableResource(getPackageManager(),
78
+                SharedResources.DRAWABLE_LOCALE_BORDER));
79
+
80
+        final Cursor cursor = getContentResolver().query(ContextApi.Places.CONTENT_URI,
81
+                new String[] { ContextApi.Places.ColumnNames._ID,
82
+                ContextApi.Places.ColumnNames.NAME }, null, null, null);
83
+
84
+        final int idColumn = cursor.getColumnIndex(ContextApi.Places.ColumnNames._ID);
85
+        final int nameColumn = cursor.getColumnIndex(ContextApi.Places.ColumnNames.NAME);
86
+        
87
+        final Spinner spinner = (Spinner) findViewById(R.id.spinner);
88
+        spinner.setAdapter(new CursorAdapter(this, cursor) {
89
+
90
+            @Override
91
+            public View newView(Context context, Cursor cursor, ViewGroup parent) {
92
+                final View view = View.inflate(context,
93
+                        android.R.layout.simple_spinner_item, null);
94
+                
95
+                bindView(view, context, cursor);
96
+
97
+                return view;
98
+            }
99
+
100
+            @Override
101
+            public void bindView(View view, Context context, Cursor cursor) {
102
+                ((TextView) view.findViewById(android.R.id.text1)).setText(
103
+                        cursor.getString(nameColumn));
104
+                ((TextView) view.findViewById(android.R.id.text1)).setTag(
105
+                        cursor.getInt(idColumn));
106
+            }
107
+        });
108
+
109
+        if (savedInstanceState == null) {
110
+            final Bundle forwardedBundle = getIntent()
111
+                    .getBundleExtra(com.twofortyfouram.Intent.EXTRA_BUNDLE);
112
+
113
+            if (forwardedBundle != null) {
114
+                int target = forwardedBundle.getInt("destination");
115
+                for (int i = 0; i < spinner.getCount(); i++) {
116
+                    Cursor value = (Cursor) spinner.getItemAtPosition(i);
117
+                    if (value.getInt(idColumn) == target) {
118
+                        spinner.setSelection(i);
119
+                        break;
120
+                    }
121
+                }
122
+
123
+            }
124
+        }
125
+    }
126
+
127
+    /**
128
+     * {@inheritDoc}
129
+     */
130
+    @Override
131
+    public void finish() {
132
+        if (isCancelled) {
133
+            FlurryAgent.onEvent("cancel");
134
+            setResult(RESULT_CANCELED);
135
+        } else {
136
+            FlurryAgent.onEvent("save");
137
+            final Intent returnIntent = new Intent();
138
+
139
+            final Bundle storeAndForwardExtras = new Bundle();
140
+
141
+            Cursor cursor = (Cursor) ((Spinner) findViewById(R.id.spinner)).getSelectedItem();
142
+            storeAndForwardExtras.putInt("destination",
143
+                    cursor.getInt(cursor.getColumnIndex(ContextApi.Places.ColumnNames._ID)));
144
+            returnIntent.putExtra(com.twofortyfouram.Intent.EXTRA_STRING_BLURB,
145
+                    cursor.getString(cursor.getColumnIndex(ContextApi.Places.ColumnNames.NAME)));
146
+
147
+            returnIntent.putExtra(com.twofortyfouram.Intent.EXTRA_BUNDLE, storeAndForwardExtras);
148
+            setResult(RESULT_OK, returnIntent);
149
+        }
150
+
151
+        FlurryAgent.onEndSession(this);
152
+        super.finish();
153
+    }
154
+
155
+    /**
156
+     * {@inheritDoc}
157
+     */
158
+    @Override
159
+    public boolean onCreateOptionsMenu(final Menu menu) {
160
+        super.onCreateOptionsMenu(menu);
161
+
162
+        final PackageManager manager = getPackageManager();
163
+
164
+        final Intent helpIntent = new Intent(com.twofortyfouram.Intent.ACTION_HELP);
165
+        helpIntent.putExtra(com.twofortyfouram.Intent.EXTRA_STRING_HELP_URL,
166
+                "http://chris.smith.name/android/contextanalyser/");
167
+
168
+        helpIntent.putExtra(com.twofortyfouram.Intent.EXTRA_STRING_BREADCRUMB, getTitle());
169
+
170
+        menu.add(SharedResources.getTextResource(manager, SharedResources.STRING_MENU_HELP))
171
+                .setIcon(SharedResources.getDrawableResource(manager,
172
+                SharedResources.DRAWABLE_MENU_HELP)).setIntent(helpIntent);
173
+        menu.add(0, MENU_DONT_SAVE, 0, SharedResources.getTextResource(manager,
174
+                SharedResources.STRING_MENU_DONTSAVE)).setIcon(SharedResources
175
+                .getDrawableResource(manager, SharedResources.DRAWABLE_MENU_DONTSAVE)).getItemId();
176
+        menu.add(0, MENU_SAVE, 0, SharedResources.getTextResource(manager,
177
+                SharedResources.STRING_MENU_SAVE)).setIcon(SharedResources
178
+                .getDrawableResource(manager, SharedResources.DRAWABLE_MENU_SAVE)).getItemId();
179
+
180
+        return true;
181
+    }
182
+
183
+    /**
184
+     * {@inheritDoc}
185
+     */
186
+    @Override
187
+    public boolean onMenuItemSelected(final int featureId, final MenuItem item) {
188
+        switch (item.getItemId()) {
189
+            case MENU_SAVE: {
190
+                finish();
191
+                return true;
192
+            }
193
+            case MENU_DONT_SAVE: {
194
+                isCancelled = true;
195
+                finish();
196
+                return true;
197
+            }
198
+        }
199
+
200
+        return super.onOptionsItemSelected(item);
201
+    }
202
+}

+ 6
- 5
code/LocalePlugin/src/uk/co/md87/android/contextanalyser/locale/R.java Bestand weergeven

@@ -22,15 +22,16 @@ public final class R {
22 22
     }
23 23
     public static final class string {
24 24
         public static final int app_name=0x7f040000;
25
-        public static final int plugin_dialog_install=0x7f040003;
25
+        public static final int location_name=0x7f040002;
26
+        public static final int plugin_dialog_install=0x7f040004;
26 27
         /**  "Install" should match up with the plugin_dialog_install string 
27 28
          */
28
-        public static final int plugin_dialog_market_not_available=0x7f040006;
29
-        public static final int plugin_dialog_need_locale_informative=0x7f040005;
30
-        public static final int plugin_dialog_need_locale_message=0x7f040004;
29
+        public static final int plugin_dialog_market_not_available=0x7f040007;
30
+        public static final int plugin_dialog_need_locale_informative=0x7f040006;
31
+        public static final int plugin_dialog_need_locale_message=0x7f040005;
31 32
         /**  NOTE: these names cannot be changed without also changing the string constants in com.twofortyfouram.MarketActivity.  You will NOT get a compile time error 
32 33
          */
33
-        public static final int plugin_dialog_title=0x7f040002;
34
+        public static final int plugin_dialog_title=0x7f040003;
34 35
         public static final int plugin_name=0x7f040001;
35 36
     }
36 37
 }

+ 49
- 11
code/LocalePlugin/src/uk/co/md87/android/contextanalyser/locale/Receiver.java Bestand weergeven

@@ -27,9 +27,13 @@ import android.content.Context;
27 27
 import android.content.Intent;
28 28
 import android.database.Cursor;
29 29
 import android.os.Bundle;
30
+import android.util.Log;
31
+import java.util.HashMap;
32
+import java.util.Map;
30 33
 
31 34
 import uk.co.md87.android.contextapi.ContextApi.Activities;
32 35
 import uk.co.md87.android.contextapi.ContextApi.Intents;
36
+import uk.co.md87.android.contextapi.ContextApi.Predictions;
33 37
 
34 38
 /**
35 39
  * Receives broadcast intents from Locale and the Context Analyser.
@@ -43,29 +47,63 @@ public class Receiver extends BroadcastReceiver {
43 47
         if (com.twofortyfouram.Intent.ACTION_QUERY_CONDITION.equals(intent.getAction())) {
44 48
             final Bundle bundle = intent.getBundleExtra(com.twofortyfouram.Intent.EXTRA_BUNDLE);
45 49
 
46
-            if (bundle == null || !bundle.containsKey("activity")) {
50
+            if (bundle == null) {
47 51
                 return;
48 52
             }
49 53
 
50
-            final Cursor cursor = context.getContentResolver().query(
51
-                    Activities.CONTENT_URI,
52
-                    new String[] { Activities.ColumnNames.ACTIVITY }, null, null, null);
54
+            if (bundle.containsKey("activity")) {
55
+                final Cursor cursor = context.getContentResolver().query(
56
+                        Activities.CONTENT_URI,
57
+                        new String[] { Activities.ColumnNames.ACTIVITY }, null, null, null);
53 58
 
54
-            if (cursor.moveToFirst()) {
55
-                final String activity = cursor.getString(
56
-                        cursor.getColumnIndex(Activities.ColumnNames.ACTIVITY));
59
+                if (cursor.moveToFirst()) {
60
+                    final String activity = cursor.getString(
61
+                            cursor.getColumnIndex(Activities.ColumnNames.ACTIVITY));
57 62
 
58
-                if (activity.equals(bundle.getString("activity"))) {
63
+                    if (activity.equals(bundle.getString("activity"))) {
64
+                        setResultCode(com.twofortyfouram.Intent.RESULT_CONDITION_SATISFIED);
65
+                    } else {
66
+                        setResultCode(com.twofortyfouram.Intent.RESULT_CONDITION_UNSATISFIED);
67
+                    }
68
+                } else {
69
+                    setResultCode(com.twofortyfouram.Intent.RESULT_CONDITION_UNKNOWN);
70
+                }
71
+            } else if (bundle.containsKey("destination")) {
72
+                final Cursor cursor = context.getContentResolver().query(
73
+                        Predictions.CONTENT_URI,
74
+                        new String[] { Predictions.ColumnNames.PLACE,
75
+                        Predictions.ColumnNames.COUNT}, null, null, null);
76
+                
77
+                final int placeColumn = cursor.getColumnIndex(Predictions.ColumnNames.PLACE);
78
+                final int countColumn = cursor.getColumnIndex(Predictions.ColumnNames.COUNT);
79
+                final Map<Integer, Integer> predictions = new HashMap<Integer, Integer>();
80
+
81
+                int max = 0, total = 0;
82
+                if (cursor.moveToFirst()) {
83
+                    do {
84
+                        int value = cursor.getInt(countColumn);
85
+                        predictions.put(cursor.getInt(placeColumn), value);
86
+                        max = Math.max(max, value);
87
+                        total += value;
88
+                    } while (cursor.moveToNext());
89
+                }
90
+
91
+                if (predictions.containsKey(bundle.getInt("destination"))
92
+                        && predictions.get(bundle.getInt("destination")) == max) {
59 93
                     setResultCode(com.twofortyfouram.Intent.RESULT_CONDITION_SATISFIED);
60 94
                 } else {
61 95
                     setResultCode(com.twofortyfouram.Intent.RESULT_CONDITION_UNSATISFIED);
62 96
                 }
63
-            } else {
64
-                setResultCode(com.twofortyfouram.Intent.RESULT_CONDITION_UNKNOWN);
65 97
             }
66 98
         } else if (Intents.ACTIVITY_CHANGED.equals(intent.getAction())) {
67 99
             final Intent broadcast = new Intent(com.twofortyfouram.Intent.ACTION_REQUEST_QUERY);
68
-            broadcast.putExtra(com.twofortyfouram.Intent.EXTRA_ACTIVITY, EditActivity.class.getName());
100
+            broadcast.putExtra(com.twofortyfouram.Intent.EXTRA_ACTIVITY,
101
+                    EditActivity.class.getName());
102
+            context.sendBroadcast(intent);
103
+        } else if (Intents.CONTEXT_CHANGED.equals(intent.getAction())) {
104
+            final Intent broadcast = new Intent(com.twofortyfouram.Intent.ACTION_REQUEST_QUERY);
105
+            broadcast.putExtra(com.twofortyfouram.Intent.EXTRA_ACTIVITY,
106
+                    EditDestination.class.getName());
69 107
             context.sendBroadcast(intent);
70 108
         }
71 109
     }

Laden…
Annuleren
Opslaan