Commit 2964fe03 authored by untidylamp's avatar untidylamp
Browse files

Initial commit

parents
Pipeline #45962831 failed with stages
in 2 minutes and 21 seconds
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "ca.winterrealm.MatrixTexting"
minSdkVersion 24
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
package ca.winterrealm.MatrixTexting;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("ca.winterrealm.MatrixTexting", appContext.getPackageName());
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ca.winterrealm.MatrixTexting">
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_MMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="ca.winterrealm.MatrixTexting.BroadcastListener">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
<intent-filter>
<action android:name="android.provider.Telephony.WAP_PUSH_RECEIVED" />
<data android:mimeType="application/vnd.wap.mms-message" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<service android:name="ca.winterrealm.MatrixTexting.MmsService" />
<service android:name="ca.winterrealm.MatrixTexting.MatrixService" />
</application>
</manifest>
\ No newline at end of file
package ca.winterrealm.MatrixTexting;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.telephony.TelephonyManager;
import android.util.Log;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
public class BroadcastListener extends BroadcastReceiver {
private static final String LOG_TAG = "ca.winterrealm.MatrixTexting.BroadcastListener";
private static final String ACTION_MMS_RECEIVED = "android.provider.Telephony.WAP_PUSH_RECEIVED";
private static final String MMS_DATA_TYPE = "application/vnd.wap.mms-message";
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Log.i(LOG_TAG, "received sms");
onRecieveSms(context, intent);
} else if (intent.getAction().equals(ACTION_MMS_RECEIVED) && intent.getType().equals(MMS_DATA_TYPE)) {
Log.i(LOG_TAG, "received mms");
onRecieveMms(context, intent);
} else {
Log.e(LOG_TAG, "received unsupported action: "+ intent.getAction() );
}
}
private void onRecieveSms(Context context, Intent intent) {
Map<String, String> msg = null;
SmsMessage[] msgs = null;
Bundle bundle = intent.getExtras();
long newSmsId = 0;
if (bundle != null && bundle.containsKey("pdus")) {
Object[] pdus = (Object[]) bundle.get("pdus");
SqlOpenHelper mSqlOpenHelper = new SqlOpenHelper(context);
if (pdus != null) {
int nbrOfpdus = pdus.length;
msg = new HashMap<String, String>(nbrOfpdus);
msgs = new SmsMessage[nbrOfpdus];
// Send long SMS of same sender in one message
for (int i = 0; i < nbrOfpdus; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
String number = msgs[i].getOriginatingAddress();
// Check if index with number exists
if (!msg.containsKey(number)) {
// Index with number doesn't exist
long sourceEpoch = msgs[i].getTimestampMillis();
long recievedEpoch= System.currentTimeMillis();
String message = msgs[i].getMessageBody();
Log.i(LOG_TAG, "Import new SMS: " + number );
newSmsId = dbManager.importNewSms(mSqlOpenHelper, dbManager.TYPE_INCOMING, sourceEpoch, recievedEpoch, number, message, dbManager.BRIDGED_NO, dbManager.LOCK_NO, dbManager.ERROR_NO );
msg.put(number, msgs[i].getMessageBody());
} else {
if (newSmsId > 0) {
// Number is there.
String previousparts = msg.get(number);
String msgString = previousparts + msgs[i].getMessageBody();
msg.put(number, msgString);
Log.i(LOG_TAG, "Update Long SMS: " + number );
dbManager.updateLongSmsBody(mSqlOpenHelper, newSmsId, msgString);
}
}
}
}
mSqlOpenHelper.close();
}
}
private void onRecieveMms(final Context context, Intent intent) {
//I don't know how to download MMSC from cell provider. so we will do something with mmssms.sb
try {
Bundle bundle = intent.getExtras();
if (bundle != null) {
byte[] buffer = bundle.getByteArray("data");
String bufferString = new String(buffer);
Log.i(LOG_TAG, "bufferString: " + bufferString);
SqlOpenHelper mSqlOpenHelper = new SqlOpenHelper(context);
//Get Phone Number (between + and /TYPE
int mmsIndex = bufferString.indexOf("/TYPE");
int mmsIndexPlus = bufferString.indexOf("+");
String number = bufferString.substring(mmsIndexPlus, mmsIndex);
//Got phone number, Remove +1
number = number.replace("+1", "");
Log.i(LOG_TAG, "Phone Number: " + number);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
Log.i(LOG_TAG, "Checking if MMS is downloaded.");
// this code will be executed after 15 seconds
Intent intnet = new Intent(context, ca.winterrealm.MatrixTexting.MmsService.class);
intnet.putExtra("onRecieveMms", true);
context.startService(intnet);
}
}, 15000);
}
} catch (Exception e ) {
Log.e(LOG_TAG, "received mms failed " + e);
}
}
}
package ca.winterrealm.MatrixTexting;
import android.provider.BaseColumns;
public class DatabaseContract {
private static final String LOG_TAG = "ca.winterrealm.MatrixTexting.DatabaseContract";
private DatabaseContract () {}
public static final class SmsEntry implements BaseColumns {
public static final String TABLE_NAME = "sms";
public static final String COLUMN_TYPE = "type";
//matrix -> cell=1 ; cell -> matrix = 2
public static final String COLUMN_SOURCE_EPOCH = "source_epoch";
//epoch in ms from sending party
public static final String COLUMN_RECIEVED_EPOCH = "received_epoch";
//epoch in ms when the device on onRecieve
public static final String COLUMN_NUMBER = "number";
//Who the message is from
public static final String COLUMN_MESSAGE = "message";
//Body of the message
public static final String COLUMN_BRIDGED = "bridged";
//Was it sent successfully?
public static final String COLUMN_LOCK = "lock";
//Are we doing something with this message?
public static final String COLUMN_ERROR = "error";
//Was there an error doing something?
public static final String SQL_CREATE_TABLE =
"CREATE TABLE " + TABLE_NAME + " (" +
_ID + " INTEGER PRIMARY KEY, " +
COLUMN_TYPE + " INTEGER NOT NULL, " +
COLUMN_SOURCE_EPOCH + " INTEGER, " +
COLUMN_RECIEVED_EPOCH + " INTEGER NOT NULL, " +
COLUMN_NUMBER + " TEXT NOT NULL, " +
COLUMN_MESSAGE + " TEXT NOT NULL, " +
COLUMN_BRIDGED + " INTEGER NOT NULL, " +
COLUMN_LOCK + " INTEGER NOT NULL, " +
COLUMN_ERROR + " INTEGER NOT NULL )";
}
public static final class MmsEntry implements BaseColumns {
public static final String TABLE_NAME = "mms";
public static final String COLUMN_TYPE = "type";
//matrix -> cell=1 ; cell -> matrix = 2
public static final String COLUMN_SOURCE_EPOCH = "source_epoch";
//epoch in ms from sending party
public static final String COLUMN_RECIEVED_EPOCH = "received_epoch";
//epoch in ms when the device on onRecieve
public static final String COLUMN_NUMBER = "number";
//Who the message is from
public static final String COLUMN_MMS_ID = "mms_id";
//ID of mmssms.db - addr
public static final String COLUMN_BRIDGED = "bridged";
//Was it sent successfully?
public static final String COLUMN_LOCK = "lock";
//Are we doing something with this message?
public static final String COLUMN_ERROR = "error";
//Was there an error doing something?
public static final String SQL_CREATE_TABLE =
"CREATE TABLE " + TABLE_NAME + " (" +
_ID + " INTEGER PRIMARY KEY, " +
COLUMN_TYPE + " INTEGER NOT NULL, " +
COLUMN_SOURCE_EPOCH + " INTEGER, " +
COLUMN_RECIEVED_EPOCH + " INTEGER NOT NULL, " +
COLUMN_NUMBER + " TEXT NOT NULL, " +
COLUMN_MMS_ID + " INTEGER UNIQUE NOT NULL, " +
COLUMN_BRIDGED + " INTEGER NOT NULL, " +
COLUMN_LOCK + " INTEGER, " +
COLUMN_ERROR + " INTEGER )";
}
}
package ca.winterrealm.MatrixTexting;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import static android.Manifest.permission.INTERNET;
import static android.Manifest.permission.READ_SMS;
import static android.Manifest.permission.RECEIVE_MMS;
import static android.Manifest.permission.RECEIVE_SMS;
import static android.Manifest.permission.SEND_SMS;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG = "ca.winterrealm.MatrixTexting.MainActivity";
private static final String[] PERMISSIONS_REQUIRED = new String[]{ READ_SMS, SEND_SMS, RECEIVE_MMS, RECEIVE_SMS, WRITE_EXTERNAL_STORAGE, INTERNET};
private static final int PERMISSION_REQUEST_CODE = 200;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (!checkPermission()) {
requestPermission();
}
Object startButton = findViewById(R.id.start_button);
((View) startButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View startServiceView) {
Log.i(LOG_TAG, "startButton - onClick");
Intent mmsIntent = new Intent(getApplicationContext(), ca.winterrealm.MatrixTexting.MmsService.class);
mmsIntent.putExtra("startButton", true);
startService(mmsIntent);
Log.i(LOG_TAG, "startButton - onClick done");
}
});
final Object stopButton = findViewById(R.id.stop_button);
((View) stopButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(LOG_TAG, "stopButton - onClick");
Intent mmsIntent = new Intent(getApplicationContext(), ca.winterrealm.MatrixTexting.MmsService.class);
stopService(mmsIntent);
Log.i(LOG_TAG, "stopButton - onClick done");
}
});
final Object testButton = findViewById(R.id.test_button);
((View) testButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(LOG_TAG, "testButton - onClick");
Intent mmsIntent = new Intent(getApplicationContext(), ca.winterrealm.MatrixTexting.MmsService.class);
mmsIntent.putExtra("testButton", true);
//startService(mmsIntent);
Intent MatrixIntent = new Intent(getApplicationContext(), ca.winterrealm.MatrixTexting.MatrixService.class);
MatrixIntent.putExtra("testButton", true);
startService(MatrixIntent);
Log.i(LOG_TAG, "testButton - onClick done");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private boolean checkPermission() {
//for each permission
for (String permission: PERMISSIONS_REQUIRED) {
int result = ContextCompat.checkSelfPermission(getApplicationContext(), permission);
if (result != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
private void requestPermission() {
ActivityCompat.requestPermissions(this, PERMISSIONS_REQUIRED, PERMISSION_REQUEST_CODE);
}
}
package ca.winterrealm.MatrixTexting;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MatrixService extends Service {
private static final String LOG_TAG = "ca.winterrealm.MatrixTexting.MatrixService";
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Boolean testing = intent.getBooleanExtra("testButton", false);
if (testing) {
Log.i(LOG_TAG, "test button pushed");
testSomeStuff();
} else {
Log.e(LOG_TAG, "Something started MmsService?");
//we shouldn't get here
}
return START_NOT_STICKY;
}
//START TESTING START
private void testSomeStuff() {
Log.i(LOG_TAG, "testSomeStuff ");
}
//End TESTING END
@Override
public void onDestroy() {
Log.i(LOG_TAG, "onDestroy: MatrixService");
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
package ca.winterrealm.MatrixTexting;
import android.app.Service;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.IBinder;
import android.util.Log;
public class MmsService extends Service {
private static final String LOG_TAG = "ca.winterrealm.MatrixTexting.MmsService";
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(LOG_TAG, "onStartCommand: MmsService");
Boolean firstStart = intent.getBooleanExtra("startButton", false);
Boolean newMms = intent.getBooleanExtra("onRecieveMms", false);
Boolean testing = intent.getBooleanExtra("testButton", false);
if (firstStart) {
Log.i(LOG_TAG, "App starting");
firstBoot();
} else if (newMms) {
Log.i(LOG_TAG, "New MMS from BroadcastListener.");
checkForNewMms();
} else if (testing) {
Log.i(LOG_TAG, "test button pushed");
//testSomeStuff();
checkForNewMms();
} else {
Log.e(LOG_TAG, "Something started MmsService?");
//we shouldn't get here
}
return START_NOT_STICKY;
}
private void firstBoot() {
SqlOpenHelper mSqlOpenHelper = new SqlOpenHelper(getApplicationContext());
long mmsCount = dbManager.countMmses(mSqlOpenHelper);
if (mmsCount == 0) {
Log.i(LOG_TAG, "Importing latest MMS to our DB as starting point");
importLatestMms(mSqlOpenHelper);
}
mSqlOpenHelper.close();
}
private void checkForNewMms() {
//can we lock here?
SqlOpenHelper mSqlOpenHelper = new SqlOpenHelper(this);
long lastBridgedMms = dbManager.lastMmsId(mSqlOpenHelper);
long latestMms = getLatestMms(mSqlOpenHelper);
Log.i(LOG_TAG, "Last MMS ID in our database: " + lastBridgedMms + " Latest MMS ID: " + latestMms);
if (lastBridgedMms > 0 && latestMms > lastBridgedMms) {
Log.i(LOG_TAG, "there is a new MMS to send to Matrix!");
findNewMms(lastBridgedMms);
} else {
Log.i(LOG_TAG, "Looks like our databases are in sync.");