From bfc20ccaf724b50785524ede65146d6373fddec4 Mon Sep 17 00:00:00 2001
From: SubashNS <nsubash02@gmail.com>
Date: Fri, 17 Feb 2017 21:26:50 +0530
Subject: [PATCH] added trusted contacts

---
 .idea/misc.xml                                |   2 +-
 app/src/main/AndroidManifest.xml              |  13 +-
 .../keepyousafe/ContactSelection.java         | 230 ++++++++++++++++++
 .../speedyapps/keepyousafe/MainActivity.java  |  32 ++-
 .../res/layout/activity_contact_selection.xml |  47 ++++
 app/src/main/res/layout/activity_main.xml     |  23 +-
 6 files changed, 331 insertions(+), 16 deletions(-)
 create mode 100644 app/src/main/java/com/speedyapps/keepyousafe/ContactSelection.java
 create mode 100644 app/src/main/res/layout/activity_contact_selection.xml

diff --git a/.idea/misc.xml b/.idea/misc.xml
index 04c63e4..ba75a42 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -53,7 +53,7 @@
     <ConfirmationsSetting value="0" id="Add" />
     <ConfirmationsSetting value="0" id="Remove" />
   </component>
-  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
     <output url="file://$PROJECT_DIR$/build/classes" />
   </component>
   <component name="ProjectType">
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 9b72289..a8ea64a 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -5,7 +5,9 @@
     <uses-permission android:name="android.permission.SEND_SMS" />
     <uses-permission android:name="android.permission.READ_CONTACTS" />
     <uses-permission android:name="android.permission.RECEIVE_SMS">
-    <uses-permission android:name="android.permission.READ_SMS" /></uses-permission>
+        <uses-permission android:name="android.permission.READ_SMS" />
+    </uses-permission>
+
     <application
         android:allowBackup="true"
         android:icon="@mipmap/ic_launcher"
@@ -24,16 +26,19 @@
             android:name=".alarmService"
             android:enabled="true"
             android:exported="true" />
-        <receiver android:name="com.speedyapps.keepyousafe.SMSReader$readSMS">
+
+        <receiver android:name=".SMSReader$readSMS">
             <intent-filter>
                 <action android:name="android.provider.Telephony.SMS_RECEIVED" />
             </intent-filter>
         </receiver>
+
         <service
             android:name=".SMSReader"
             android:enabled="true"
-            android:exported="true">
-        </service>
+            android:exported="true"></service>
+
+        <activity android:name=".ContactSelection"></activity>
     </application>
 
 </manifest>
\ No newline at end of file
diff --git a/app/src/main/java/com/speedyapps/keepyousafe/ContactSelection.java b/app/src/main/java/com/speedyapps/keepyousafe/ContactSelection.java
new file mode 100644
index 0000000..a7d302a
--- /dev/null
+++ b/app/src/main/java/com/speedyapps/keepyousafe/ContactSelection.java
@@ -0,0 +1,230 @@
+package com.speedyapps.keepyousafe;
+
+import android.app.Activity;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.database.Cursor;
+import android.net.Uri;
+import android.os.Bundle;
+import android.preference.PreferenceManager;
+import android.provider.ContactsContract;
+import android.util.Log;
+import android.util.SparseBooleanArray;
+import android.view.ActionMode;
+import android.view.Menu;
+import android.view.MenuItem;
+import android.view.View;
+import android.widget.AbsListView;
+import android.widget.ArrayAdapter;
+import android.widget.EditText;
+import android.widget.ListView;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.Map;
+
+public class ContactSelection extends Activity {
+    private static final int RESULT_PICK_CONTACT = 85500;
+    public static String file1 = "MyPREFERENCES";
+    public static String file2 = "PREFERENCES";
+    public static String file3 = "COUNT";
+
+
+    private TextView textView1;
+    SharedPreferences sharedpreferences, sp, shared, get, no, get1;
+    SharedPreferences.Editor ed, editor, num;
+    private TextView textView2;
+    ArrayList<String> a;
+    ListView l;
+    ArrayAdapter arr;
+    public static int times = 0;
+    int count, indexName, indexNumber;
+    String n;
+    String n1;
+    String name;
+    String number;
+
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_contact_selection);
+        //textView1 = (TextView) findViewById(R.id.textView1);
+        //textView2 = (TextView) findViewById(R.id.textView2);
+        l = (ListView) findViewById(R.id.list);
+        sharedpreferences = getSharedPreferences(file1, Context.MODE_PRIVATE);
+        sp = getSharedPreferences(file2, Context.MODE_PRIVATE);
+        shared = getSharedPreferences(file3, Context.MODE_PRIVATE);
+        editor = sharedpreferences.edit();
+        ed = sp.edit();
+        num = shared.edit();
+
+
+    }
+
+
+    public void pickContact(View v) {
+        Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
+        startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
+    }
+
+
+    @Override
+    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+        // check whether the result is ok
+        if (resultCode == RESULT_OK) {
+            // Check for the request code, we might be usign multiple startActivityForReslut
+            switch (requestCode) {
+                case RESULT_PICK_CONTACT:
+                    contactPicked(data);
+                    break;
+            }
+        } else {
+            Log.e("MainActivity", "Failed to pick contact");
+        }
+
+    }
+
+
+    private void contactPicked(Intent data) {
+        Cursor cursor = null;
+        times = shared.getInt("count", 0);
+        Log.d("times", String.valueOf(times));
+
+
+        // getData() method will have the Content Uri of the selected contact
+        Uri uri = data.getData();
+        ContentResolver cr = getContentResolver();
+        Cursor cur = cr.query(uri, null, null, null, null);
+        String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
+                ContactsContract.CommonDataKinds.Phone.NUMBER};
+        Cursor names = getContentResolver().query(uri, projection, null, null, null);
+        indexName = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
+        indexNumber = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
+
+        names.moveToLast();
+        do {
+            name = names.getString(indexName);
+
+            number = names.getString(indexNumber);
+
+            editor.putString(name, number);
+            editor.apply();
+            editor.commit();
+            if (editor.commit()) {
+
+                Log.d("subtimes1", Integer.toString(times));
+                times++;
+                Log.d("subtimes2", Integer.toString(times));
+                num.putInt("count", times);
+                num.apply();
+                num.commit();
+                Log.d("name", name);
+
+
+                ed.putString("value" + times, name);
+                ed.apply();
+                ed.commit();
+
+
+                Toast.makeText(this, "Successfully Added", Toast.LENGTH_SHORT).show();
+            } else {
+                Toast.makeText(this, "not entered", Toast.LENGTH_SHORT).show();
+            }
+
+        }
+        while (names.moveToNext());
+
+    }
+
+    public void retrive(View v)
+    {
+        try {
+            count = shared.getInt("count", 0);
+            a = new ArrayList<>();
+            arr = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, a);
+            l.setAdapter(arr);
+            Log.d("subcount", String.valueOf(count));
+
+            for (int i = 1; i <= count; i++) {
+                n1 = sp.getString("value" + i, "");
+                n = sharedpreferences.getString(n1, "");
+                Log.d("subn1", n1);
+                Log.d("subn", n);
+
+                try
+                {
+                    if(!(n1.isEmpty()&&n.isEmpty()))
+                    {
+                        a.add(n1 + "(" + n + ")");
+                        arr.notifyDataSetChanged();
+                        Log.d("sub", String.valueOf(a));
+                    }
+                }
+                catch (Exception e){}
+
+            }
+        } catch (Exception e) {}
+
+    }
+
+
+    public void delete(View v)
+    {
+        int pos = l.getCheckedItemPosition();
+        if (pos > -1)
+        {
+            try {
+                String b,b1 = null,b2;
+                b = a.get(pos).toString();
+
+                String s11 = b.split("\\(")[0];
+                Log.d("del", s11);
+
+
+                Iterator iter = sp.getAll().entrySet().iterator();
+
+                {
+                    while (iter.hasNext()) {
+                        Map.Entry pair = (Map.Entry) iter.next();
+                        if (s11.equals(pair.getValue())) {
+                            b1 = pair.getKey().toString();
+                            Log.d("b1", b1);
+                        }
+                    }
+                    // Check the value here
+
+                    a.remove(pos);
+
+                    arr.notifyDataSetChanged();
+
+                    sp.edit().remove(b1).commit();
+                    sharedpreferences.edit().remove(s11).commit();
+
+
+
+
+
+                    Toast.makeText(this, "Deleted Successfully", Toast.LENGTH_SHORT).show();
+                }
+            } catch (Exception e) {
+            }
+
+
+
+        }
+        else
+        {
+            Toast.makeText(this, "Not Deleted", Toast.LENGTH_SHORT).show();
+        }
+        arr.notifyDataSetChanged();
+
+    }
+
+}
+
diff --git a/app/src/main/java/com/speedyapps/keepyousafe/MainActivity.java b/app/src/main/java/com/speedyapps/keepyousafe/MainActivity.java
index c6742f0..74bf1de 100644
--- a/app/src/main/java/com/speedyapps/keepyousafe/MainActivity.java
+++ b/app/src/main/java/com/speedyapps/keepyousafe/MainActivity.java
@@ -1,6 +1,8 @@
 package com.speedyapps.keepyousafe;
 
+import android.content.Context;
 import android.content.Intent;
+import android.content.SharedPreferences;
 import android.support.annotation.MainThread;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
@@ -9,11 +11,19 @@ import android.util.Log;
 import android.view.View;
 
 public class MainActivity extends AppCompatActivity {
-    Intent intent;int choice;
+    Intent intent;int choice,count;
+    String n,n1;
+    public static String file1 = "MyPREFERENCES";
+    public static String file2 = "PREFERENCES";
+    public static String file3 = "COUNT";
+    SharedPreferences sharedpreferences,sp,shared;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
+        sharedpreferences = getSharedPreferences(file1, Context.MODE_PRIVATE);
+        sp = getSharedPreferences(file2, Context.MODE_PRIVATE);
+        shared = getSharedPreferences(file3, Context.MODE_PRIVATE);
         intent = new Intent(MainActivity.this,alarmService.class);
         choice=0;
         Intent serviceIntent = new Intent(MainActivity.this,SMSReader.class);
@@ -24,7 +34,13 @@ public class MainActivity extends AppCompatActivity {
         Log.i("zz",""+choice);
         switch(choice) {
             case 0:  startService(intent);
-                sendSMS("8606670880","Help Me!!!!");
+                count = shared.getInt("count", 0);
+                for (int i = 1; i <= count; i++)
+                {
+                    n1 = sp.getString("value" + i, "");
+                    n = sharedpreferences.getString(n1, "");
+                sendSMS(n,"Help Me!!!!");
+                }
                 break;
             case 1: stopService(intent);
                 break;
@@ -32,9 +48,17 @@ public class MainActivity extends AppCompatActivity {
 
         choice=(choice+1)%2;
     }
+    public void contact(View v)
+    {
+
+        Intent i=new Intent(this,ContactSelection.class);
+        startActivity(i);
+
+    }
     public void sendSMS(String phoneNumber, String message)
     {
-        SmsManager sms = SmsManager.getDefault();
-        sms.sendTextMessage(phoneNumber, null, message, null, null);
+            SmsManager sms = SmsManager.getDefault();
+            sms.sendTextMessage(phoneNumber, null, message, null, null);
+
     }
 }
diff --git a/app/src/main/res/layout/activity_contact_selection.xml b/app/src/main/res/layout/activity_contact_selection.xml
new file mode 100644
index 0000000..928006f
--- /dev/null
+++ b/app/src/main/res/layout/activity_contact_selection.xml
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="fill_parent"
+    android:layout_height="fill_parent"
+    android:orientation="vertical"
+    android:padding="5dp" >
+
+<Button
+        android:id="@+id/button1"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:onClick="pickContact"
+        android:text="Pick Contact" />
+
+    <ListView
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:layout_alignParentLeft="true"
+        android:layout_alignParentStart="true"
+        android:choiceMode="singleChoice"
+        android:id="@+id/list"
+        android:layout_above="@+id/delete"
+        android:layout_below="@+id/view" />
+
+    <Button
+        android:text="View Contacts"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:id="@+id/view"
+        android:layout_below="@+id/button1"
+        android:layout_alignParentLeft="true"
+        android:layout_alignParentStart="true"
+        android:onClick="retrive" />
+
+    <Button
+        android:text="Delete"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:id="@+id/delete"
+        android:textSize="18sp"
+        android:onClick="delete"
+        android:layout_alignParentBottom="true"
+        android:layout_centerHorizontal="true" />
+
+
+</RelativeLayout>
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index 186100e..1339bf6 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -14,18 +14,28 @@
     tools:context="com.speedyapps.keepyousafe.MainActivity">
 
     <TextView
-        android:text="Keep You Safe!"
+        android:text="Keep Me Safe!"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
-        android:layout_marginTop="16dp"
         android:id="@+id/keepYouSafeText"
         android:textSize="30sp"
         android:textStyle="normal|bold"
         android:textAlignment="center"
         android:textColor="#FFFFFF"
+        android:layout_above="@+id/helpButton"
+        android:layout_alignLeft="@+id/helpButton"
+        android:layout_alignStart="@+id/helpButton"
+        android:layout_marginBottom="18dp" />
+
+    <Button
+        android:text="Contacts"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:id="@+id/pick"
+        android:layout_alignParentRight="true"
+        android:layout_alignParentEnd="true"
         android:layout_alignParentTop="true"
-        android:layout_alignParentLeft="true"
-        android:layout_alignParentStart="true" />
+        android:onClick="contact" />
 
     <ImageButton
         app:srcCompat="@drawable/help"
@@ -33,10 +43,9 @@
         android:scaleType="fitXY"
         android:layout_width="350dp"
         android:layout_height="350dp"
+        android:onClick="onClick"
         android:layout_alignParentBottom="true"
         android:layout_alignParentRight="true"
-        android:layout_alignParentEnd="true"
-        android:layout_marginBottom="37dp"
-        android:onClick="onClick" />
+        android:layout_alignParentEnd="true" />
 
 </RelativeLayout>
-- 
GitLab