Skip to content

Commit

Permalink
Finish feature/OP-768
Browse files Browse the repository at this point in the history
OP-768: Policies: Remove MANAGE_EXTERNAL_STORAGE permission and convert all features to use appropriate file APIs
  • Loading branch information
dragos-dobre authored May 24, 2022
2 parents 288f7cb + 619a8f2 commit e9d0844
Show file tree
Hide file tree
Showing 31 changed files with 1,093 additions and 1,427 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static def getDate() {
}

android {
compileSdkVersion 30
compileSdkVersion 32
buildToolsVersion '30.0.3'
if ( keystorePropertiesFile.exists() ) {
signingConfigs {
Expand All @@ -35,7 +35,7 @@ android {
defaultConfig {
applicationId "org.openimis.imispolicies"
minSdkVersion 19
targetSdkVersion 30
targetSdkVersion 32
versionCode gitVersionCode
versionName gitVersionName
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand Down
8 changes: 5 additions & 3 deletions app/src/debug/java/org/openimis/imispolicies/tools/Log.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.openimis.imispolicies.tools;

import android.content.Context;
import android.net.Uri;
import android.support.v4.content.FileProvider;

import org.openimis.imispolicies.AppInformation;
import org.openimis.imispolicies.BuildConfig;
import org.openimis.imispolicies.Global;
import org.openimis.imispolicies.util.ZipUtils;

import java.io.File;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -88,21 +90,21 @@ private static void log(String tag, String msg, int level) {
}
}

public static void zipLogFiles() {
public static void zipLogFiles(Context context) {
File cacheDir = Global.getContext().getExternalCacheDir();
File[] logFiles = cacheDir.listFiles((dir, filename) -> filename.startsWith(logFilePrefix));
File targetFile = new File(cacheDir, logExportFileName);

if (logFiles != null) {
ArrayList<File> filesToZip = new ArrayList<>(Arrays.asList(logFiles));
Compressor.zip(filesToZip, targetFile, "");
ZipUtils.zipFiles(filesToZip, targetFile, "");
}

Uri logExportUri = FileProvider.getUriForFile(Global.getContext(),
String.format("%s.fileprovider", BuildConfig.APPLICATION_ID),
targetFile);

Global.getGlobal().sendFile(logExportUri, "application/zip");
Global.getGlobal().sendFile(context, logExportUri, "application/octet-stream");
}

public static void deleteLogFiles() {
Expand Down
49 changes: 48 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

<!-- These statements are required as these permissions will be included otherwise -->
<uses-permission
Expand All @@ -23,6 +22,9 @@
<uses-permission
android:name="android.permission.READ_CONTACTS"
tools:node="remove" />
<uses-permission
android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:node="remove" />

<uses-feature
android:name="android.hardware.camera"
Expand Down Expand Up @@ -62,6 +64,7 @@
<activity
android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:exported="true"
android:label="@string/app_name_policies"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar">
Expand Down Expand Up @@ -131,6 +134,50 @@
<activity android:name=".SearchNotEnrolledPolicies" />
<activity android:name=".NotEnrolledPoliciesOverview" />
<activity android:name=".BulkControlNumbersActivity" />

<!-- Override missing configuration for library -->
<activity
android:name="com.google.zxing.client.android.CaptureActivity"
android:exported="false"
tools:node="merge"
tools:overrideLibrary="com.google.zxing.client" />
<activity
android:name="com.google.zxing.client.android.encode.EncodeActivity"
android:exported="false"
tools:node="merge"
tools:overrideLibrary="com.google.zxing.client" />
<activity
android:name="com.google.zxing.client.android.book.SearchBookContentsActivity"
android:exported="false"
tools:node="merge"
tools:overrideLibrary="com.google.zxing.client" />
<activity
android:name="com.google.zxing.client.android.share.ShareActivity"
android:exported="false"
tools:node="merge"
tools:overrideLibrary="com.google.zxing.client" />
<activity
android:name="com.google.zxing.client.android.history.HistoryActivity"
android:exported="false"
tools:node="merge"
tools:overrideLibrary="com.google.zxing.client" />
<activity
android:name="com.google.zxing.client.android.share.BookmarkPickerActivity"
android:exported="false"
tools:node="merge"
tools:overrideLibrary="com.google.zxing.client" />
<activity
android:name="com.google.zxing.client.android.share.AppPickerActivity"
android:exported="false"
tools:node="merge"
tools:overrideLibrary="com.google.zxing.client" />
<activity
android:name="com.google.zxing.client.android.HelpActivity"
android:exported="false"
tools:node="merge"
tools:overrideLibrary="com.google.zxing.client" />


</application>

</manifest>
6 changes: 3 additions & 3 deletions app/src/main/assets/pages/Sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ $(document).ready(function () {

break;
case "liCreateRenewalXML":
Android.zipFeedBackRenewal('Renewal');
Android.CreateRenewalExport();
break;
case "liUploadFeedback":
if (!Android.isLoggedIn()) {
window.open("Login.html?s=0", "_self");
} else {
Android.UploadOfflineFeedbackRenewal('feedback');
Android.uploadFeedbacks();
}
break;
case "liCreateFeedbackXML":
Android.zipFeedBackRenewal('Feedback');
Android.CreateFeedbackExport();
break;

case "liDownloadMasterData":
Expand Down
98 changes: 44 additions & 54 deletions app/src/main/java/org/openimis/imispolicies/Acquire.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;

import org.openimis.imispolicies.tools.ImageManager;
import org.openimis.imispolicies.tools.Log;
import org.openimis.imispolicies.tools.StorageManager;
import org.openimis.imispolicies.util.FileUtils;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
Expand All @@ -64,15 +69,13 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Acquire extends AppCompatActivity {
private static final String LOG_TAG = "ACQUIRE";
private static final int SCAN_QR_REQUEST_CODE = 0;
private static final int TAKE_PHOTO_REQUEST_CODE = 1;
private static final String TEMP_PHOTO_PATH = "images/acquireTemp.jpg";

private Global global;

Expand All @@ -85,17 +88,15 @@ public class Acquire extends AppCompatActivity {
private String Path = null;
private int result = 0;

private String msg = "";
private double Longitude, Latitude;
private LocationManager lm;
private String towers;
private ClientAndroidInterface ca;
private SQLHandler sqlHandler;

private File tempPhotoFile;
private Uri tempPhotoUri;

private Picasso picasso;
private StorageManager storageManager;

private final Target imageTarget = new Target() {
@Override
Expand Down Expand Up @@ -127,37 +128,26 @@ public void onCreate(Bundle savedInstanceState) {

global = (Global) getApplicationContext();
ca = new ClientAndroidInterface(this);

picasso = new Picasso.Builder(this).build();
storageManager = StorageManager.of(this);
sqlHandler = new SQLHandler(this);

Path = global.getSubdirectory("Images") + "/";
tempPhotoFile = new File(Path, "temp.jpg");
try {
if (tempPhotoFile.delete()) {
Log.v(LOG_TAG, "Leftover temp image deleted");
}
etCHFID = findViewById(R.id.etCHFID);
iv = findViewById(R.id.imageView);
btnTakePhoto = findViewById(R.id.btnTakePhoto);
btnScan = findViewById(R.id.btnScan);
btnSubmit = findViewById(R.id.btnSubmit);

if (!tempPhotoFile.createNewFile()) {
Log.w(LOG_TAG, "Temp photo file already exists");
}
File tempPhotoFile = FileUtils.createTempFile(this, TEMP_PHOTO_PATH);
if (tempPhotoFile != null) {
tempPhotoUri = FileProvider.getUriForFile(this,
String.format("%s.fileprovider", BuildConfig.APPLICATION_ID),
tempPhotoFile);
if (tempPhotoUri == null) {
Log.w(LOG_TAG, "Failed to create temp photo URI");
}
} catch (IOException e) {
Log.e(LOG_TAG, "Temp photo file creation failed", e);
}

etCHFID = findViewById(R.id.etCHFID);
iv = findViewById(R.id.imageView);
btnTakePhoto = findViewById(R.id.btnTakePhoto);
btnScan = findViewById(R.id.btnScan);
btnSubmit = findViewById(R.id.btnSubmit);

sqlHandler = new SQLHandler(this);

etCHFID.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Expand All @@ -168,14 +158,14 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
}

@Override
public void afterTextChanged(Editable InsNo) {
String path = null;
if (!InsNo.toString().isEmpty()) {
path = ca.GetListOfImagesContain(InsNo.toString());
public void afterTextChanged(Editable text) {
File photoFile = null;
String insureeNumber = text.toString();
if (!insureeNumber.isEmpty()) {
photoFile = ImageManager.of(Acquire.this).getNewestInsureeImage(insureeNumber);
}
if (path != null && !"".equals(path)) {
File file = new File(path);
picasso.load(file)
if (photoFile != null) {
picasso.load(photoFile)
.placeholder(R.drawable.person)
.error(R.drawable.person)
.into(iv);
Expand Down Expand Up @@ -217,13 +207,17 @@ public void afterTextChanged(Editable InsNo) {
});

btnTakePhoto.setOnClickListener(v -> {
try {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, tempPhotoUri);
global.grantUriPermissions(this, tempPhotoUri, intent, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivityForResult(intent, TAKE_PHOTO_REQUEST_CODE);
} catch (ActivityNotFoundException e) {
Log.e(LOG_TAG, "Image capture activity not found", e);
if (!etCHFID.getText().toString().isEmpty()) {
try {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, tempPhotoUri);
global.grantUriPermissions(this, tempPhotoUri, intent, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivityForResult(intent, TAKE_PHOTO_REQUEST_CODE);
} catch (ActivityNotFoundException e) {
Log.e(LOG_TAG, "Image capture activity not found", e);
}
} else {
Toast.makeText(this, R.string.MissingCHFID, Toast.LENGTH_LONG).show();
}
});

Expand Down Expand Up @@ -256,15 +250,6 @@ public void afterTextChanged(Editable InsNo) {
}

runOnUiThread(() -> {
switch (result) {
case 1:
msg = getResources().getString(R.string.PhotoSaved);
break;
default:
msg = getResources().getString(R.string.CouldNotUpload);
break;
}

Toast.makeText(Acquire.this, getResources().getString(R.string.PhotoSaved), Toast.LENGTH_LONG).show();

etCHFID.setText("");
Expand All @@ -280,10 +265,7 @@ public void afterTextChanged(Editable InsNo) {
@Override
protected void onDestroy() {
super.onDestroy();

if (!tempPhotoFile.delete()) {
Log.w(LOG_TAG, "Temp photo file deletion failed");
}
FileUtils.removeTempFile(this, TEMP_PHOTO_PATH);
}

@Override
Expand Down Expand Up @@ -331,6 +313,8 @@ private int SubmitData() throws IOException, UserException {
String date = AppInformation.DateTimeInfo.getDefaultFileDatetimeFormatter().format(new Date());
String fName = etCHFID.getText() + "_" + global.getOfficerCode() + "_" + date + "_" + Latitude + "_" + Longitude + ".jpg";

File[] oldInsureeImages = ImageManager.of(this).getInsureeImages(etCHFID.getText().toString());

File file = new File(global.getSubdirectory("Images"), fName);
if (file.exists()) {
Log.w(LOG_TAG, String.format("File already exists: %s", file.getAbsolutePath()));
Expand All @@ -342,13 +326,19 @@ private int SubmitData() throws IOException, UserException {

if (file.length() == 0L) {
Log.w(LOG_TAG, "Compressing photo failed, the resulting file has no content");
if (!file.delete()) {
Log.w(LOG_TAG, "Deleting empty output file failed");
}
return 0;
} else {
FileUtils.deleteFiles(oldInsureeImages);
}

ContentValues contentValues = new ContentValues();
contentValues.put("PhotoPath", file.getAbsolutePath());
String[] whereArgs = {etCHFID.getText().toString()};

if(sqlHandler.updateData("tblInsuree", contentValues, "CHFID = ?", whereArgs, false) == 0) {
if (sqlHandler.updateData("tblInsuree", contentValues, "CHFID = ?", whereArgs, false) == 0) {
Log.w(LOG_TAG, String.format("Cannot update photo path. No insuree for CHFID: %s", etCHFID.getText().toString()));
}

Expand Down
Loading

0 comments on commit e9d0844

Please sign in to comment.