mirror of
https://github.com/nerzhul/ownCloud-SMS-App.git
synced 2025-06-07 07:56:14 +00:00
Permit to restore messages, but it's not finished yet
Remaining problems: * date is not properly retrieved on the phone * no feedback when connectivity errors * no feedback when processing * no feedback when finishing
This commit is contained in:
parent
45d7352c32
commit
e06377d64b
@ -6,11 +6,10 @@
|
||||
<uses-sdk android:maxSdkVersion="25"/>
|
||||
|
||||
<uses-permission android:name="android.permission.READ_SMS"/>
|
||||
<!-- For SMS Restore -->
|
||||
<!-- <uses-permission android:name="android.permission.WRITE_SMS"/> -->
|
||||
<uses-permission android:name="android.permission.WRITE_SMS" />
|
||||
|
||||
<!-- For SMS Sending -->
|
||||
<!-- <uses-permission android:name="android.permission.SEND_SMS"/> -->
|
||||
<!-- For SMS Restore & Sending -->
|
||||
<uses-permission android:name="android.permission.SEND_SMS"/>
|
||||
|
||||
<!-- For SMS Broadcaster -->
|
||||
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
|
||||
@ -72,11 +71,51 @@
|
||||
android:resource="@xml/owncloud_account_authenticator"/>
|
||||
</service>
|
||||
|
||||
<receiver android:name=".broadcast_receivers.IncomingSms">
|
||||
<receiver android:name=".broadcast_receivers.IncomingSms"
|
||||
android:permission="android.permission.BROADCAST_SMS">
|
||||
<intent-filter>
|
||||
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
|
||||
<action android:name="android.provider.Telephony.SMS_DELIVER" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
|
||||
<!-- BroadcastReceiver that listens for incoming MMS messages. Note: useless class, used only for restoring SMS -->
|
||||
<receiver android:name=".misc.MmsReceiver"
|
||||
android:permission="android.permission.BROADCAST_WAP_PUSH">
|
||||
<intent-filter>
|
||||
<action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
|
||||
<data android:mimeType="application/vnd.wap.mms-message" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
|
||||
<!-- Activity that allows the user to send new SMS/MMS messages Note: useless class, used only for restoring SMS -->
|
||||
<activity android:name=".misc.ComposeSmsActivity" >
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.SEND" />
|
||||
<action android:name="android.intent.action.SENDTO" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
<category android:name="android.intent.category.BROWSABLE" />
|
||||
<data android:scheme="sms" />
|
||||
<data android:scheme="smsto" />
|
||||
<data android:scheme="mms" />
|
||||
<data android:scheme="mmsto" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<!-- Service that delivers messages from the phone "quick response" Note: useless class, used only for restoring SMS -->
|
||||
<service android:name=".misc.HeadlessSmsSendService"
|
||||
android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
|
||||
android:exported="true" >
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
<data android:scheme="sms" />
|
||||
<data android:scheme="smsto" />
|
||||
<data android:scheme="mms" />
|
||||
<data android:scheme="mmsto" />
|
||||
</intent-filter>
|
||||
</service>
|
||||
|
||||
<receiver android:name=".broadcast_receivers.ConnectivityChanged">
|
||||
<intent-filter>
|
||||
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
|
||||
|
@ -20,10 +20,18 @@ package fr.unix_experience.owncloud_sms.activities.remote_account;
|
||||
import android.Manifest;
|
||||
import android.accounts.Account;
|
||||
import android.accounts.AccountManager;
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Bundle;
|
||||
import android.provider.Telephony;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
|
||||
import fr.unix_experience.owncloud_sms.R;
|
||||
import fr.unix_experience.owncloud_sms.engine.ASyncSMSRecovery;
|
||||
@ -31,12 +39,15 @@ import fr.unix_experience.owncloud_sms.engine.ASyncSMSRecovery;
|
||||
public class RestoreMessagesActivity extends AppCompatActivity {
|
||||
|
||||
Account _account = null;
|
||||
String _defaultSmsApp;
|
||||
private static final int REQUEST_DEFAULT_SMSAPP = 1;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_restore_messages);
|
||||
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
|
||||
// Change message to define Android 4.4 or greated is required
|
||||
// @TODO Change message to define Android 4.4 or greated is required
|
||||
return;
|
||||
}
|
||||
|
||||
@ -47,7 +58,7 @@ public class RestoreMessagesActivity extends AppCompatActivity {
|
||||
// accountName cannot be null, devel error
|
||||
assert accountName != null;
|
||||
AccountManager accountManager = AccountManager.get(getBaseContext());
|
||||
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
|
||||
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
|
||||
// TODO: Consider calling
|
||||
// ActivityCompat#requestPermissions
|
||||
// here to request the missing permissions, and then overriding
|
||||
@ -68,7 +79,72 @@ public class RestoreMessagesActivity extends AppCompatActivity {
|
||||
throw new IllegalStateException(getString(R.string.err_didnt_find_account_restore));
|
||||
}
|
||||
|
||||
new ASyncSMSRecovery.SMSRecoveryTask(getApplicationContext(), _account).execute();
|
||||
_defaultSmsApp = Telephony.Sms.getDefaultSmsPackage(this);
|
||||
TextView tv = (TextView) findViewById(R.id.tv_error_default_smsapp);
|
||||
Button fix_button = (Button) findViewById(R.id.button_fix_permissions);
|
||||
final Button launch_restore = (Button) findViewById(R.id.button_launch_restore);
|
||||
final ProgressBar pb = (ProgressBar) findViewById(R.id.progressbar_restore);
|
||||
pb.setVisibility(View.INVISIBLE);
|
||||
|
||||
if (!Telephony.Sms.getDefaultSmsPackage(this).equals(getPackageName())) {
|
||||
_defaultSmsApp = Telephony.Sms.getDefaultSmsPackage(getBaseContext());
|
||||
tv.setVisibility(View.VISIBLE);
|
||||
fix_button.setVisibility(View.VISIBLE);
|
||||
launch_restore.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
else {
|
||||
tv.setVisibility(View.INVISIBLE);
|
||||
fix_button.setVisibility(View.INVISIBLE);
|
||||
launch_restore.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
fix_button.setOnClickListener(new View.OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
|
||||
// @TODO Change message to define Android 4.4 or greated is required
|
||||
return;
|
||||
}
|
||||
|
||||
Log.i(RestoreMessagesActivity.TAG, "Ask to change the default SMS app");
|
||||
|
||||
Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
|
||||
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, getBaseContext().getPackageName());
|
||||
startActivityForResult(intent, REQUEST_DEFAULT_SMSAPP);
|
||||
}
|
||||
});
|
||||
|
||||
launch_restore.setOnClickListener(new View.OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
launch_restore.setVisibility(View.INVISIBLE);
|
||||
pb.setVisibility(View.VISIBLE);
|
||||
Log.i(RestoreMessagesActivity.TAG, "Launching restore asynchronously");
|
||||
new ASyncSMSRecovery.SMSRecoveryTask(getApplicationContext(), _account).execute();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
/*Intent finalIntent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
|
||||
finalIntent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, _defaultSmsApp);
|
||||
startActivity(finalIntent);*/
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
switch (requestCode) {
|
||||
case RestoreMessagesActivity.REQUEST_DEFAULT_SMSAPP:
|
||||
Log.i(RestoreMessagesActivity.TAG, "RC: " + Integer.toString(resultCode));
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
TextView tv = (TextView) findViewById(R.id.tv_error_default_smsapp);
|
||||
Button fix_button = (Button) findViewById(R.id.button_fix_permissions);
|
||||
Button launch_restore = (Button) findViewById(R.id.button_launch_restore);
|
||||
tv.setVisibility(View.INVISIBLE);
|
||||
fix_button.setVisibility(View.INVISIBLE);
|
||||
launch_restore.setVisibility(View.VISIBLE);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static final String TAG = RestoreMessagesActivity.class.getSimpleName();
|
||||
|
@ -1,13 +1,19 @@
|
||||
package fr.unix_experience.owncloud_sms.engine;
|
||||
|
||||
import android.accounts.Account;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.util.Log;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import fr.unix_experience.owncloud_sms.enums.MailboxID;
|
||||
|
||||
/*
|
||||
* Copyright (c) 2014-2016, Loic Blot <loic.blot@unix-experience.fr>
|
||||
*
|
||||
@ -43,10 +49,33 @@ public interface ASyncSMSRecovery {
|
||||
}
|
||||
Log.i(ASyncSMSRecovery.TAG, "Starting background recovery");
|
||||
|
||||
// Verify connectivity
|
||||
|
||||
Long start = (long) 0;
|
||||
JSONObject obj = new OCSMSOwnCloudClient(_context, _account).retrieveSomeMessages(start, 500);
|
||||
if (obj == null) {
|
||||
Log.i(ASyncSMSRecovery.TAG, "Retrieved returns failure");
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
JSONObject messages = obj.getJSONObject("messages");
|
||||
Iterator<?> keys = messages.keys();
|
||||
while(keys.hasNext()) {
|
||||
String key = (String)keys.next();
|
||||
if (messages.get(key) instanceof JSONObject) {
|
||||
JSONObject msg = messages.getJSONObject(key);
|
||||
ContentValues values = new ContentValues();
|
||||
values.put("address", msg.getString("address"));
|
||||
values.put("body", msg.getString("msg"));
|
||||
values.put("date", Long.parseLong(key));
|
||||
|
||||
MailboxID mailbox_id = MailboxID.fromInt(msg.getInt("mailbox"));
|
||||
_context.getContentResolver().insert(Uri.parse(mailbox_id.getURI()), values);
|
||||
}
|
||||
}
|
||||
|
||||
while (obj.getLong("last_id") != start) {
|
||||
Log.i(TAG, obj.toString());
|
||||
start = obj.getLong("last_id");
|
||||
obj = new OCSMSOwnCloudClient(_context, _account).retrieveSomeMessages(start, 500);
|
||||
}
|
||||
|
@ -244,6 +244,7 @@ public class OCSMSOwnCloudClient {
|
||||
JSONObject retrieveSomeMessages(Long start, Integer limit) {
|
||||
// This is not allowed by server
|
||||
if (limit > OCSMSOwnCloudClient.SERVER_RECOVERY_MSG_LIMIT) {
|
||||
Log.e(OCSMSOwnCloudClient.TAG, "Message recovery limit exceeded");
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -251,10 +252,13 @@ public class OCSMSOwnCloudClient {
|
||||
doHttpRequest(_http.getMessages(start, limit));
|
||||
} catch (OCSyncException e) {
|
||||
_jsonQueryBuffer = null;
|
||||
Log.e(OCSMSOwnCloudClient.TAG, "Request failed.");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!_jsonQueryBuffer.has("messages") || !_jsonQueryBuffer.has("last_id")) {
|
||||
Log.e(OCSMSOwnCloudClient.TAG,
|
||||
"Invalid response received from server, either messages or last_id field is missing.");
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -26,12 +26,33 @@ package fr.unix_experience.owncloud_sms.enums;
|
||||
*/
|
||||
|
||||
public enum MailboxID {
|
||||
INBOX("content://sms/inbox"),
|
||||
SENT("content://sms/sent"),
|
||||
DRAFTS("content://sms/drafts"),
|
||||
ALL("content://sms");
|
||||
INBOX(0),
|
||||
SENT(1),
|
||||
DRAFTS(2),
|
||||
ALL(3);
|
||||
|
||||
MailboxID(final String uri) { this.uri = uri; }
|
||||
MailboxID(int id) {
|
||||
switch (id) {
|
||||
case 0: uri = "content://sms/inbox"; break;
|
||||
case 1: uri = "content://sms/sent"; break;
|
||||
case 2: uri = "content://sms/drafts"; break;
|
||||
case 3: uri = "content://sms"; break;
|
||||
default: throw new AssertionError();
|
||||
}
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public static MailboxID fromInt(int id) {
|
||||
switch (id) {
|
||||
case 0: return MailboxID.INBOX;
|
||||
case 1: return MailboxID.SENT;
|
||||
case 2: return MailboxID.DRAFTS;
|
||||
case 3: return MailboxID.ALL;
|
||||
default: throw new AssertionError();
|
||||
}
|
||||
}
|
||||
private final String uri;
|
||||
public String getURI() { return this.uri; }
|
||||
private final int id;
|
||||
public int getId() { return id; }
|
||||
public String getURI() { return uri; }
|
||||
}
|
||||
|
@ -0,0 +1,6 @@
|
||||
package fr.unix_experience.owncloud_sms.misc;
|
||||
|
||||
import android.app.Activity;
|
||||
|
||||
public class ComposeSmsActivity extends Activity {
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package fr.unix_experience.owncloud_sms.misc;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import android.os.IBinder;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
public class HeadlessSmsSendService extends Service {
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package fr.unix_experience.owncloud_sms.misc;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
public class MmsReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
|
||||
}
|
||||
}
|
@ -36,12 +36,50 @@
|
||||
android:paddingTop="@dimen/activity_vertical_margin"
|
||||
tools:context="fr.unix_experience.owncloud_sms.activities.remote_account.RestoreMessagesActivity">
|
||||
|
||||
<TextView
|
||||
<ProgressBar
|
||||
style="?android:attr/progressBarStyleLarge"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textAppearance="?android:attr/textAppearanceLarge"
|
||||
android:text="@string/feature_not_already_implemented"
|
||||
android:id="@+id/textView"
|
||||
android:id="@+id/progressbar_restore"
|
||||
android:layout_centerVertical="true"
|
||||
android:layout_centerHorizontal="true"/>
|
||||
|
||||
<TextView
|
||||
android:text="@string/error_make_default_sms_app"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="41dp"
|
||||
android:id="@+id/tv_error_default_smsapp"
|
||||
android:layout_alignParentTop="true"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:textColor="@color/holo_red_light"
|
||||
android:textAlignment="center"/>
|
||||
|
||||
<Button
|
||||
android:text="@string/fix_permissions"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/tv_error_default_smsapp"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:layout_marginTop="32dp"
|
||||
android:id="@+id/button_fix_permissions"
|
||||
style="@style/Widget.AppCompat.Button"/>
|
||||
|
||||
<Button
|
||||
android:text="@string/launch_restore"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/button_launch_restore"
|
||||
style="@style/Widget.AppCompat.Button.Colored"
|
||||
android:textSize="24sp"
|
||||
android:textAllCaps="false"
|
||||
android:paddingStart="25dp"
|
||||
android:paddingEnd="25dp"
|
||||
android:paddingBottom="15dp"
|
||||
android:paddingTop="15dp"
|
||||
android:fontFamily="sans-serif-medium"
|
||||
android:layout_alignBaseline="@+id/button_fix_permissions"
|
||||
android:layout_alignBottom="@+id/button_fix_permissions"
|
||||
android:layout_centerHorizontal="true"/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
@ -247,4 +247,7 @@
|
||||
<string name="feature_not_already_implemented">This feature is not already implemented and will be available soon.</string>
|
||||
<string name="pref_title_minimum_sync_chars">Minimum phonenumber length</string>
|
||||
<string name="err_didnt_find_account_restore">We didn\'t find your account to restore your message, this is a very strange situation.</string>
|
||||
<string name="launch_restore">Restore my SMS</string>
|
||||
<string name="error_make_default_sms_app">Please make this application default SMS application to permit restore your messages.</string>
|
||||
<string name="fix_permissions">Fix permissions</string>
|
||||
</resources>
|
||||
|
Loading…
x
Reference in New Issue
Block a user