Whitelist (alpha)

This commit is contained in:
Alessandro Ferro 2022-10-25 07:48:05 +02:00
parent 24c10fdddc
commit 945c145133
16 changed files with 331 additions and 34 deletions

View File

@ -0,0 +1,11 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="#FFFFFF"
android:alpha="0.8">
<path
android:fillColor="@android:color/white"
android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
</vector>

Binary file not shown.

After

Width:  |  Height:  |  Size: 178 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 120 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 159 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 260 B

View File

@ -23,6 +23,13 @@
android:supportsRtl="true"
android:theme="@style/Theme.LocateMyDevice"
tools:targetApi="30">
<activity
android:name=".WhitelistContactsActivity"
android:exported="false">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity
android:name=".RingerActivity"
android:exported="false">

View File

@ -67,39 +67,6 @@ public class MainActivity extends AppCompatActivity {
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// Grant Background location if and only if FINE_LOCATION is already granted
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION},
Utils.PERMISSION_ACCESS_BACKGROUND_LOCATION);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.settings_button) {
Intent myIntent = new Intent(MainActivity.this, SettingsActivity.class);
MainActivity.this.startActivity(myIntent);
}
return super.onOptionsItemSelected(item);
}
public void displayDrawOverlayPermissionDialog(){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Display over other apps");
@ -129,4 +96,39 @@ public class MainActivity extends AppCompatActivity {
" permission to provide the 'lock' feature.");
startActivity(intent);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// Grant Background location if and only if FINE_LOCATION is already granted
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION},
Utils.PERMISSION_ACCESS_BACKGROUND_LOCATION);
}
}
}
// Add settings button on the top
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_settings, menu);
return super.onCreateOptionsMenu(menu);
}
// Settings button click
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.settings_button) {
Intent myIntent = new Intent(MainActivity.this, SettingsActivity.class);
MainActivity.this.startActivity(myIntent);
}
return super.onOptionsItemSelected(item);
}
}

View File

@ -6,6 +6,7 @@ import android.content.SharedPreferences;
public class Settings {
public static final int PASSWORD = 0;
public static final int SMS_COMMAND = 1;
public static final int WHITELIST_ENABLED = 2;
private final SharedPreferences sharedPreferences;
@ -29,6 +30,8 @@ public class Settings {
return CipherUtils.get256Sha("0000");
case SMS_COMMAND:
return "LMD";
case WHITELIST_ENABLED:
return "false";
}
return null;
}

View File

@ -1,13 +1,17 @@
package com.xfarrow.locatemydevice;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
@ -17,6 +21,9 @@ public class SettingsActivity extends AppCompatActivity {
private Button buttonEnterPin;
private EditText editTextLmdCommand;
@SuppressLint("UseSwitchCompatOrMaterialCode")
private Switch whitelistSwitch;
private Button addContactsButton;
private Settings settings;
@Override
@ -24,6 +31,7 @@ public class SettingsActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
settings = new Settings(this);
setViews();
setValues();
setListeners();
@ -32,6 +40,11 @@ public class SettingsActivity extends AppCompatActivity {
private void setViews(){
buttonEnterPin = findViewById(R.id.buttonEnterPassword);
editTextLmdCommand = findViewById(R.id.editTextLmdCommand);
whitelistSwitch = findViewById(R.id.SwitchWhitelist);
addContactsButton = findViewById(R.id.buttonAddContacts);
addContactsButton.setEnabled(Boolean.parseBoolean(settings.get(Settings.WHITELIST_ENABLED)));
whitelistSwitch.setChecked(Boolean.parseBoolean(settings.get(Settings.WHITELIST_ENABLED)));
}
private void setValues(){
@ -105,5 +118,22 @@ public class SettingsActivity extends AppCompatActivity {
}
}
});
whitelistSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
addContactsButton.setEnabled(isChecked);
settings.set(Settings.WHITELIST_ENABLED, String.valueOf(isChecked));
}
});
addContactsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(SettingsActivity.this, WhitelistContactsActivity.class);
SettingsActivity.this.startActivity(myIntent);
}
});
}
}

View File

@ -50,6 +50,12 @@ public class SmsHandler {
String providedOption = "";
String providedPassword = "";
// Deny communication to those not in the whitelist, if enabled
WhitelistDbHandler whitelistDbHandler = new WhitelistDbHandler(context);
if(Boolean.parseBoolean(settings.get(Settings.WHITELIST_ENABLED)) && !whitelistDbHandler.isContactPresent(sender)){
return;
}
String regexToMatch = "^"
+ command
+ "\\s"

View File

@ -0,0 +1,89 @@
package com.xfarrow.locatemydevice;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class WhitelistContactsActivity extends AppCompatActivity {
ListView contactsListView;
ArrayList<String> contacts;
ArrayAdapter<String> listviewAdapter;
WhitelistDbHandler whitelistDbHandler = new WhitelistDbHandler(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_whitelist_contacts);
setTitle("Whitelist");
setViews();
contacts = whitelistDbHandler.getAllContacts();
listviewAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
contacts);
contactsListView.setAdapter(listviewAdapter);
}
private void setViews(){
contactsListView = findViewById(R.id.ContactsListView);
}
// Add "add" button on the top
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_add, menu);
return super.onCreateOptionsMenu(menu);
}
// "Add contact" click
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.add_button) {
Intent pickContact = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
pickContact.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(pickContact, 123);
}
return super.onOptionsItemSelected(item);
}
// Gets fired when the user has chosen the contact
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == 123) {
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
int phoneIndex = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = c.getString(phoneIndex).trim().replace("\\s+", "");
contactSelected(number);
}
c.close();
}
}
private void contactSelected(String number){
if(whitelistDbHandler.isContactPresent(number)){
Toast.makeText(this, "Contact already in the list", Toast.LENGTH_SHORT).show();
return;
}
whitelistDbHandler.addContact(number);
contacts.add(number);
listviewAdapter.notifyDataSetChanged();
}
}

View File

@ -0,0 +1,83 @@
package com.xfarrow.locatemydevice;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
public class WhitelistDbHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "WhitelistDb";
private static final String TABLE_CONTACTS = "Contacts";
private static final String KEY_ID = "id";
private static final String KEY_PH_NO = "phone_number";
public WhitelistDbHandler(@Nullable Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " " + "INTEGER PRIMARY KEY,"
+ KEY_PH_NO + " " + "TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
onCreate(db);
}
// Add a contact
void addContact(String phoneNo){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_PH_NO, phoneNo); // Contact Phone
db.insert(TABLE_CONTACTS, null, values);
db.close(); // Closing database connection
}
// Deleting single contact
public void deleteContact(String phoneNo) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CONTACTS, KEY_ID + " = ?", new String[] { phoneNo });
db.close();
}
public ArrayList<String> getAllContacts() {
ArrayList<String> array_list = new ArrayList<String>();
//hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery( "select * from " + TABLE_CONTACTS, null );
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(KEY_PH_NO);
while(!cursor.isAfterLast()){
array_list.add(cursor.getString(columnIndex));
cursor.moveToNext();
}
cursor.close();
return array_list;
}
public boolean isContactPresent(String phoneNo){
SQLiteDatabase db = this.getWritableDatabase();
String query = "Select * from " + TABLE_CONTACTS + " where " + KEY_PH_NO + " = " + "\"" + phoneNo + "\"";
Cursor cursor = db.rawQuery(query, null);
int count = cursor.getCount();
cursor.close();
return count > 0;
}
}

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WhitelistContactsActivity">
<ListView
android:id="@+id/ContactsListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:divider="#AAAAAA"
android:dividerHeight="1dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -60,11 +60,48 @@
<EditText
android:id="@+id/editTextLmdCommand"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="50dp"
android:ems="10"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:text="LMD" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/listDivider" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="17sp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="Whitelist" />
<Switch
android:id="@+id/SwitchWhitelist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enabled"
android:layout_marginStart="10dp"
tools:ignore="UseSwitchCompatOrMaterialXml" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="Only contacts in the whitelist are allowed to communicate with this smartphone"/>
<Button
android:id="@+id/buttonAddContacts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:text="Add contacts"/>
</LinearLayout>
</ScrollView>

View File

@ -0,0 +1,9 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/add_button"
android:title=""
app:showAsAction="always"
android:icon="@drawable/add"
/>
</menu>