]> hydra-www.ietfng.org Git - acmetensortoys-teled/commitdiff
SMS location reporting components
authorNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Sat, 20 Aug 2016 21:54:19 +0000 (17:54 -0400)
committerNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Sat, 20 Aug 2016 21:54:19 +0000 (17:54 -0400)
Some progress towards SMS location reports.  Right now, it immediately sends
upon having a contact picked and nothing's persisted or anything like that.
Still, some progress > no progress.

mobile/src/main/java/com/acmetensortoys/android/teled/Service/EphemeralTeleDService.java
mobile/src/main/java/com/acmetensortoys/android/teled/Service/PositionReporting.java
mobile/src/main/java/com/acmetensortoys/android/teled/UI/FragPrefs.java
mobile/src/main/java/com/acmetensortoys/android/teled/UI/MainActivity.java

index ba052fcfcd59013bc4c7c16ebc496575bb7ee721..302a365c94ac822b1e34401749139cd2d97a5c5b 100644 (file)
@@ -1,10 +1,14 @@
 package com.acmetensortoys.android.teled.Service;
 
+import android.Manifest;
 import android.app.IntentService;
 import android.app.PendingIntent;
 import android.content.Intent;
 import android.content.Context;
+import android.location.Location;
+import android.location.LocationManager;
 import android.net.Uri;
+import android.telephony.SmsManager;
 import android.util.Log;
 
 /**
@@ -44,10 +48,22 @@ public class EphemeralTeleDService extends IntentService {
         if (intent != null) {
             final String action = intent.getAction();
             if (ACTION_TEST.equals(action)) {
-                Log.d("EphemeralTeleDService", "Test action: " + intent.toString() + " eb=" + intent.getExtras().toString());
+                Log.d("EphemeralTeleDService", "Test action: " + intent.toString());
+                if(intent.getExtras() != null) {
+                    Log.d("EphemeralTeleDService", " ... has extras=" + intent.getExtras().toString());
+                }
             } else if (ACTION_SEND_LOCATION_SMS.equals(action)) {
-                // final String param1 = intent.getStringExtra(EXTRA_PARAM1);
-                handleActionSendLocationSMS(intent);
+                Uri to = intent.getData();
+
+                LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
+                this.enforceCallingOrSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION,
+                    "Missing location permission");
+                Location l = lm.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
+
+                SmsManager sm = SmsManager.getDefault();
+                // XXX Hook send and delivery messages
+                sm.sendTextMessage(to.getSchemeSpecificPart(), null, "locn rep : " + l.toString(), null, null);
+
             } /* else if (ACTION_BAZ.equals(action)) {
                 final String param1 = intent.getStringExtra(EXTRA_PARAM1);
                 final String param2 = intent.getStringExtra(EXTRA_PARAM2);
@@ -55,13 +71,4 @@ public class EphemeralTeleDService extends IntentService {
             } */
         }
     }
-
-    /**
-     * Handle action Foo in the provided background thread with the provided
-     * parameters.
-     */
-    private void handleActionSendLocationSMS(Intent i) {
-        // TODO: Handle action Foo
-        throw new UnsupportedOperationException("Not yet implemented");
-    }
 }
index 4be09a9c199b58ce952fccf78cf3ae8fc7b474be..2c239054218827c66709689a7f7a9b36a61da51c 100644 (file)
@@ -5,7 +5,7 @@ import android.content.Context;
 import android.location.LocationListener;
 import android.location.LocationManager;
 
-public abstract class PositionReporting
+public class PositionReporting
 {
     public PositionReporting(Context ctx, LocationListener ll) {
         LocationManager lm = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
index fc3cc15314d8e6d5781b2b5f87fb81d0642022b8..1b87e4ca8e6e126ccb20c481af9b1478a651ffad 100644 (file)
@@ -1,8 +1,11 @@
 package com.acmetensortoys.android.teled.UI;
 
 import android.app.Activity;
+import android.app.PendingIntent;
 import android.content.Intent;
 import android.content.SharedPreferences;
+import android.database.Cursor;
+import android.net.Uri;
 import android.os.Bundle;
 import android.preference.ListPreference;
 import android.preference.Preference;
@@ -12,6 +15,7 @@ import android.provider.ContactsContract;
 import android.util.Log;
 
 import com.acmetensortoys.android.teled.R;
+import com.acmetensortoys.android.teled.Service.EphemeralTeleDService;
 
 public class FragPrefs
         extends PreferenceFragment {
@@ -36,7 +40,7 @@ public class FragPrefs
             public boolean onPreferenceClick(Preference _p) {
                 Intent conPik = new Intent(
                         Intent.ACTION_PICK,
-                        ContactsContract.Contacts.CONTENT_URI);
+                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
                 startActivityForResult(conPik, REQID_SMS_PICK);
                 return true;
             }
@@ -56,10 +60,32 @@ public class FragPrefs
         */
     }
 
+    static final String[] phone_Uri_projection = { ContactsContract.CommonDataKinds.Phone.NUMBER };
+
     @Override
     public void onActivityResult(int req, int res, Intent i) {
         if(req == REQID_SMS_PICK && res == Activity.RESULT_OK) {
-            Log.d("FragPrefs", i.toString());
+            Log.d("FragPrefs", "SMS_PICK " + i.toString());
+
+            Cursor c = getActivity().getContentResolver()
+                    .query(i.getData(), phone_Uri_projection, null, null, null );
+            if (c != null) {
+                c.moveToFirst();
+                String number = c.getString(c.getColumnIndex(phone_Uri_projection[0]));
+                c.close();
+
+                Log.d("FragPrefs", " ... which is to say: " + number);
+
+                Uri x = Uri.fromParts("smsto", number, "");
+                PendingIntent pi = EphemeralTeleDService.pendingSendLocationSMS(getActivity(), x);
+                try {
+                    pi.send();
+                } catch (PendingIntent.CanceledException ce) {
+                    Log.d("FragPrefs", "... pi cancelled?");
+                }
+            } else {
+                Log.d("FragPrefs", " ... null cursor?");
+            }
         }
     }
 }
index 96a55634d243329ab6d27b9aeb5cebc87aea4f79..eb4bc336c5b8a9b17c71f96dd39a4777d001b9b9 100644 (file)
@@ -19,6 +19,7 @@ import android.content.IntentFilter;
 import android.content.ServiceConnection;
 import android.content.ComponentName;
 import android.content.SharedPreferences;
+import android.content.pm.PackageManager;
 import android.net.Uri;
 import android.os.CancellationSignal;
 import android.os.IBinder;
@@ -42,6 +43,8 @@ public class MainActivity extends Activity
 
     private final static int PERM_REQ_IX_RECV_SMS = 1;
     private final static int PERM_REQ_IX_FINE_LOC = 2;
+    private final static int PERM_REQ_IX_EVERYTHING = 3;
+
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -101,6 +104,14 @@ public class MainActivity extends Activity
         super.onStart();
         bindService(new Intent(this, TeleDService.class), tdisSC,
                 Context.BIND_AUTO_CREATE | Context.BIND_ABOVE_CLIENT);
+
+        Log.d("Main", "Requesting permission?");
+        requestPermissions(
+                    new String[]{
+                            Manifest.permission.ACCESS_FINE_LOCATION,
+                            Manifest.permission.RECEIVE_SMS,
+                            Manifest.permission.SEND_SMS},
+                    PERM_REQ_IX_EVERYTHING);
     }
 
     @Override