check kitkat sd permission before deleting from viewpager

This commit is contained in:
tibbi 2016-11-06 19:03:59 +01:00
parent a70e99da34
commit 84f81ed3e2

View file

@ -11,6 +11,8 @@ import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.support.annotation.RequiresApi;
import android.support.v4.provider.DocumentFile;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
import android.view.Menu; import android.view.Menu;
@ -20,12 +22,14 @@ import android.view.View;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog; import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog;
import com.simplemobiletools.gallery.Config;
import com.simplemobiletools.gallery.Constants; import com.simplemobiletools.gallery.Constants;
import com.simplemobiletools.gallery.MyViewPager; import com.simplemobiletools.gallery.MyViewPager;
import com.simplemobiletools.gallery.R; import com.simplemobiletools.gallery.R;
import com.simplemobiletools.gallery.Utils; import com.simplemobiletools.gallery.Utils;
import com.simplemobiletools.gallery.adapters.MyPagerAdapter; import com.simplemobiletools.gallery.adapters.MyPagerAdapter;
import com.simplemobiletools.gallery.dialogs.RenameItemDialog; import com.simplemobiletools.gallery.dialogs.RenameItemDialog;
import com.simplemobiletools.gallery.dialogs.WritePermissionDialog;
import com.simplemobiletools.gallery.fragments.ViewPagerFragment; import com.simplemobiletools.gallery.fragments.ViewPagerFragment;
import com.simplemobiletools.gallery.models.Medium; import com.simplemobiletools.gallery.models.Medium;
@ -273,9 +277,30 @@ public class ViewPagerActivity extends SimpleActivity
mIsUndoShown = false; mIsUndoShown = false;
mBeingDeleted = ""; mBeingDeleted = "";
boolean mWasFileDeleted = false;
final File file = new File(mToBeDeleted); final File file = new File(mToBeDeleted);
if (file.delete()) { if (Utils.Companion.needsStupidWritePermissions(this, mToBeDeleted)) {
if (Config.newInstance(this).getTreeUri().isEmpty()) {
new WritePermissionDialog(this, new WritePermissionDialog.OnWritePermissionListener() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onConfirmed() {
final Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, Constants.OPEN_DOCUMENT_TREE);
}
});
} else {
final DocumentFile document = Utils.Companion.getFileDocument(this, mToBeDeleted);
if (document.canWrite()) {
mWasFileDeleted = document.delete();
}
}
} else {
mWasFileDeleted = file.delete();
}
if (mWasFileDeleted) {
mBeingDeleted = mToBeDeleted; mBeingDeleted = mToBeDeleted;
final String[] deletedPath = new String[]{mToBeDeleted}; final String[] deletedPath = new String[]{mToBeDeleted};
MediaScannerConnection.scanFile(getApplicationContext(), deletedPath, null, new MediaScannerConnection.OnScanCompletedListener() { MediaScannerConnection.scanFile(getApplicationContext(), deletedPath, null, new MediaScannerConnection.OnScanCompletedListener() {
@ -285,6 +310,7 @@ public class ViewPagerActivity extends SimpleActivity
} }
}); });
} }
mToBeDeleted = ""; mToBeDeleted = "";
mUndoBtn.setVisibility(View.GONE); mUndoBtn.setVisibility(View.GONE);
} }