Progress displayed

This commit is contained in:
jens 2021-09-19 12:06:46 +02:00
parent 165df24f0c
commit 11f5f8905f
5 changed files with 95 additions and 24 deletions

View File

@ -6,8 +6,8 @@ android {
compileSdk 30
defaultConfig {
applicationId "de.server47.smsexport"
minSdk 21
applicationId "de.server47.messageexport"
minSdk 23
targetSdk 30
versionCode 1
versionName "1.0"

View File

@ -2,11 +2,11 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.server47.messageexport">
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<!-- <uses-permission android:name="android.permission.SEND_SMS" />-->
<!-- <uses-permission android:name="android.permission.RECEIVE_SMS" />-->
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission-group.SMS" />
<!-- <uses-permission android:name="android.permission.WRITE_SMS" />-->
<!-- <uses-permission android:name="android.permission-group.SMS" />-->
<application
android:allowBackup="true"

View File

@ -1,16 +1,20 @@
package de.server47.messageexport;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
@ -33,15 +37,18 @@ public class MainActivity extends Activity
Button bExport;
RadioButton rbRcs, rbSms, rbMms;
ProgressBar pbStatus;
TextView tvStatus;
final static int typeSMS = 0;
final static int typeMMS = 1;
final static int typeRCS = 2;
final static int requestCodeExport = 815;
final static int requestCodePermissionRequest = 155;
long messageCount = 0;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
@ -53,19 +60,39 @@ public class MainActivity extends Activity
rbSms = (RadioButton)findViewById(R.id.rbSms);
rbMms = (RadioButton)findViewById(R.id.rbMms);
pbStatus = (ProgressBar)findViewById(R.id.pbStatus);
tvStatus = (TextView)findViewById(R.id.tvStatus);
bExport.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, requestCodeExport);
if(checkSelfPermission(Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED)
{
requestPermissions(new String[] { Manifest.permission.READ_SMS }, requestCodePermissionRequest);
return;
}
startExport();
}
});
}
class AsyncTaskExport extends AsyncTask<Uri, Integer, Void>
void startExport()
{
if(checkSelfPermission(Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED)
{
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, requestCodeExport);
}
else
{
tvStatus.setText(getResources().getString(R.string.permissionDenied));
Toast.makeText(MainActivity.this, getResources().getString(R.string.permissionDenied), Toast.LENGTH_LONG).show();
}
}
class AsyncTaskExport extends AsyncTask<Uri, Integer, Boolean>
{
String getExportString()
{
@ -82,7 +109,9 @@ public class MainActivity extends Activity
{
messageCount = response.size();
publishProgress((int)Math.round(messageCount/response.size()));
tvStatus.setText(String.format(getResources().getString(R.string.exporting), String.valueOf(response.size())));
publishProgress((int)Math.round(messageCount/response.size()*100));
StringBuilder export = new StringBuilder();
@ -139,15 +168,27 @@ public class MainActivity extends Activity
}
@Override
protected Void doInBackground(Uri... uriTree)
protected Boolean doInBackground(Uri... uriTree)
{
int selectedMessageType;
if(rbRcs.isChecked())
selectedMessageType = typeRCS;
else if(rbSms.isChecked())
selectedMessageType = typeSMS;
else
selectedMessageType = typeMMS;
String dataToWrite = getExportString();
if(!StringUtils.isEmpty(dataToWrite))
exportFiles(uriTree[0], dataToWrite);
return exportFiles(uriTree[0], selectedMessageType, dataToWrite);
else
{
if (Looper.myLooper() == null)
Looper.prepare();
Toast.makeText(MainActivity.this, "Error reading messages. Can\'t export.", Toast.LENGTH_SHORT).show();
}
return null;
return false;
}
@Override
@ -156,6 +197,17 @@ public class MainActivity extends Activity
super.onProgressUpdate(values);
pbStatus.setProgress(values[0]);
}
@Override
protected void onPostExecute(Boolean result)
{
super.onPostExecute(result);
if(result)
tvStatus.setText(String.format(getResources().getString(R.string.exported), String.valueOf(messageCount)));
else
tvStatus.setText(getResources().getString(R.string.errorExporting));
}
}
@Override
@ -168,19 +220,27 @@ public class MainActivity extends Activity
switch (requestCode)
{
case requestCodeExport:
if (resultCode == RESULT_OK)
{
AsyncTask<Uri,Integer,Void> exportTask = new AsyncTaskExport();
exportTask.execute(data.getData());
}
AsyncTask<Uri,Integer,Boolean> exportTask = new AsyncTaskExport();
exportTask.execute(data.getData());
break;
case requestCodePermissionRequest:
startExport();
break;
}
}
}
void exportFiles(Uri uriTree, String content)
boolean exportFiles(Uri uriTree, int messageType, String content)
{
String fileName = "Message_export_" + String.valueOf(Calendar.getInstance().getTimeInMillis()) + ".xml";
String messageTypeString;
if(messageType == typeRCS)
messageTypeString = "RCS";
else if(messageType == typeSMS)
messageTypeString = "SMS";
else
messageTypeString = "MMS";
String fileName = "Message_export_" + messageTypeString + "_" + String.valueOf(Calendar.getInstance().getTimeInMillis()) + ".xml";
DocumentFile directory = DocumentFile.fromTreeUri(this, uriTree);
@ -209,17 +269,17 @@ public class MainActivity extends Activity
OutputStream out = getApplicationContext().getContentResolver().openOutputStream(exportFile.getUri());
out.write(content.getBytes());
out.close();
Toast.makeText(MainActivity.this, "Export complete. " + String.valueOf(messageCount) + " messages have been exported.", Toast.LENGTH_LONG).show();
return true;
}
catch (IOException e)
{
e.printStackTrace();
Toast.makeText(MainActivity.this, "Error while writing file.", Toast.LENGTH_LONG).show();
}
}
// else
// Toast.makeText(MainActivity.this, getResources().getString(R.string.ConfigurationExportError), Toast.LENGTH_LONG).show();
return false;
}
List<Map<String,String>> readMessages(int messageType)
@ -250,7 +310,7 @@ public class MainActivity extends Activity
Cursor cursor = getContentResolver().query(path, null, null, null, null);
if (cursor.moveToFirst())
if (cursor != null && cursor.moveToFirst())
{
// must check the result to prevent exception
long messageCounter = 0;

View File

@ -46,11 +46,18 @@
android:minHeight="50dp"
android:max="100" />
<TextView
android:id="@+id/tvStatus"
android:gravity="center"
android:layout_marginBottom="@dimen/defaultMargin"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bExport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/defaultMargin"
android:layout_gravity="center"
android:text="Export" />

View File

@ -9,4 +9,8 @@
<string name="hello_first_fragment">Hello first fragment</string>
<string name="hello_second_fragment">Hello second fragment. Arg: %1$s</string>
<string name="permissionDenied">Permission has been denied.</string>
<string name="exporting">Exporting %1$s messages.</string>
<string name="exported">%1$s messages have been exported.</string>
<string name="errorExporting">An error occured while exporting your messages.</string>
</resources>