Skip to content

Commit

Permalink
Better DVR permission handling (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
vertexodessa authored Dec 7, 2024
1 parent 0572ae3 commit 7e66365
Showing 1 changed file with 94 additions and 25 deletions.
119 changes: 94 additions & 25 deletions app/src/main/java/com/openipc/pixelpilot/VideoActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.UriPermission;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
Expand Down Expand Up @@ -160,6 +161,68 @@ private void resetApp()
}
}

private boolean hasUriPermission(Uri uri) {
for (UriPermission perm : getContentResolver().getPersistedUriPermissions()) {
if (perm.getUri().equals(uri) && perm.isWritePermission()) {
return true;
}
}
return false;
}

private void resetFolderPermissions() {
// Retrieve the stored DVR folder URI
SharedPreferences prefs = getSharedPreferences("general", Context.MODE_PRIVATE);
String dvrFolderUriString = prefs.getString("dvr_folder_", null);
if (dvrFolderUriString == null) {
Toast.makeText(this, "No folder permissions to reset.", Toast.LENGTH_SHORT).show();
return;
}

Uri dvrUri = Uri.parse(dvrFolderUriString);

// Revoke persisted URI permissions
for (UriPermission perm : getContentResolver().getPersistedUriPermissions()) {
if (perm.getUri().equals(dvrUri)) {
getContentResolver().releasePersistableUriPermission(perm.getUri(), Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION );
Log.d(TAG, "Released URI permission for: " + perm.getUri());
}
}

// Clear the stored URI from SharedPreferences
SharedPreferences.Editor editor = prefs.edit();
editor.remove("dvr_folder_");
editor.apply();

// Stop any ongoing DVR recording
if (dvrFd != null) {
stopDvr();
}

// Update the record button icon to default
binding.imgBtnRecord.setImageResource(R.drawable.record);

// Reset any related UI elements
binding.txtRecordLabel.setVisibility(View.GONE);
binding.imgRecIndicator.setVisibility(View.INVISIBLE);

// Inform the user
Toast.makeText(this, "Folder permissions have been reset.", Toast.LENGTH_LONG).show();

// Optionally, prompt the user to select a new folder immediately
// Uncomment the following lines if you want to prompt immediately
/*
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
startActivityForResult(intent, PICK_DVR_REQUEST_CODE);
*/
}


@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -318,18 +381,7 @@ public void onStopTrackingTouch(SeekBar distanceSeekBar) { }
chart.setData(noData);

binding.imgBtnRecord.setOnClickListener(item -> {
if (dvrFd == null) {
Uri dvrUri = openDvrFile();
if (dvrUri != null) {
startDvr(dvrUri);
} else {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(intent, PICK_DVR_REQUEST_CODE);
}
} else {
stopDvr();
}
startStopDvr();
});

binding.btnSettings.setOnClickListener(v -> {
Expand Down Expand Up @@ -395,18 +447,7 @@ public void onStopTrackingTouch(SeekBar distanceSeekBar) { }
SubMenu recording = popup.getMenu().addSubMenu("Recording");
MenuItem dvrBtn = recording.add(dvrFd == null ? "Start" : "Stop");
dvrBtn.setOnMenuItemClickListener(item -> {
if (dvrFd == null) {
Uri dvrUri = openDvrFile();
if (dvrUri != null) {
startDvr(dvrUri);
} else {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(intent, PICK_DVR_REQUEST_CODE);
}
} else {
stopDvr();
}
startStopDvr();
return true;
});
MenuItem fmp4 = recording.add("fMP4");
Expand All @@ -421,6 +462,12 @@ public void onStopTrackingTouch(SeekBar distanceSeekBar) { }
return false;
});

MenuItem resetPermissions = recording.add("Reset DVR folder");
resetPermissions.setOnMenuItemClickListener(item -> {
resetFolderPermissions();
return true;
});

SubMenu help = popup.getMenu().addSubMenu("Help");
MenuItem logs = help.add("Send Logs");
logs.setOnMenuItemClickListener(item -> {
Expand Down Expand Up @@ -543,6 +590,24 @@ private Uri openDvrFile() {
return null;
}

private void startStopDvr() {
if (dvrFd == null) {
Uri dvrUri = openDvrFile();
if (dvrUri != null) {
startDvr(dvrUri);
} else {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
startActivityForResult(intent, PICK_DVR_REQUEST_CODE);
}
} else {
stopDvr();
}
}

private void startDvr(Uri dvrUri) {
if (dvrFd != null) {
stopDvr();
Expand Down Expand Up @@ -630,6 +695,11 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
Uri uri;
if (data != null && data.getData() != null) {
uri = data.getData();
final int takeFlags = data.getFlags() &
(Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION );
getContentResolver().takePersistableUriPermission(uri, takeFlags);

// Perform operations on the document using its URI.
SharedPreferences prefs = getSharedPreferences("general", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
Expand Down Expand Up @@ -743,7 +813,6 @@ protected void onResume() {
wfbLinkManager.refreshAdapters();
osdManager.restoreOSDConfig();

registerReceivers();
super.onResume();
}

Expand Down

0 comments on commit 7e66365

Please sign in to comment.