This repository has been archived by the owner on Feb 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a SU explorer sample (not tested with root access)
- Loading branch information
1 parent
c9d7035
commit cc17fc1
Showing
8 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
sample/src/main/java/com/nononsenseapps/filepicker/sample/root/SUErrorFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.nononsenseapps.filepicker.sample.root; | ||
|
||
import android.app.Dialog; | ||
import android.content.DialogInterface; | ||
import android.os.Bundle; | ||
import android.support.annotation.NonNull; | ||
import android.support.v4.app.DialogFragment; | ||
import android.support.v4.app.FragmentManager; | ||
import android.support.v7.app.AlertDialog; | ||
|
||
/** | ||
* A dialog which tells the user that no SU binary is available | ||
*/ | ||
public class SUErrorFragment extends DialogFragment { | ||
|
||
private static final String TAG = "SUErrorFragment"; | ||
|
||
public static void showDialog(@NonNull final FragmentManager fm) { | ||
SUErrorFragment d = new SUErrorFragment(); | ||
d.show(fm, TAG); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Dialog onCreateDialog(Bundle savedInstanceState) { | ||
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); | ||
builder.setMessage("No read permisson, root unavailable") | ||
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { | ||
@Override | ||
public void onClick(DialogInterface dialog, int which) { | ||
dialog.dismiss(); | ||
} | ||
}); | ||
return builder.create(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
sample/src/main/java/com/nononsenseapps/filepicker/sample/root/SUPickerActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.nononsenseapps.filepicker.sample.root; | ||
|
||
import android.os.Environment; | ||
import android.support.annotation.Nullable; | ||
|
||
import com.nononsenseapps.filepicker.AbstractFilePickerActivity; | ||
import com.nononsenseapps.filepicker.AbstractFilePickerFragment; | ||
|
||
import java.io.File; | ||
|
||
public class SUPickerActivity extends AbstractFilePickerActivity<File> { | ||
|
||
public SUPickerActivity() { | ||
super(); | ||
} | ||
|
||
@Override | ||
protected AbstractFilePickerFragment<File> getFragment(@Nullable String startPath, | ||
int mode, | ||
boolean allowMultiple, | ||
boolean allowCreateDir) { | ||
AbstractFilePickerFragment<File> fragment = new SUPickerFragment(); | ||
// startPath is allowed to be null. In that case, default folder should be SD-card and | ||
// not "/" | ||
fragment.setArgs( | ||
startPath != null ? startPath : Environment.getExternalStorageDirectory().getPath(), | ||
mode, allowMultiple, allowCreateDir); | ||
return fragment; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
sample/src/main/java/com/nononsenseapps/filepicker/sample/root/SUPickerActivity2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.nononsenseapps.filepicker.sample.root; | ||
|
||
/** | ||
* Just for second theme | ||
*/ | ||
public class SUPickerActivity2 extends SUPickerActivity { | ||
} |
71 changes: 71 additions & 0 deletions
71
sample/src/main/java/com/nononsenseapps/filepicker/sample/root/SUPickerFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.nononsenseapps.filepicker.sample.root; | ||
|
||
import android.support.annotation.NonNull; | ||
import android.util.Log; | ||
|
||
import com.nononsenseapps.filepicker.FilePickerFragment; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
|
||
import eu.chainfire.libsuperuser.Shell; | ||
|
||
/** | ||
* An example picker which calls out to LibSU to get Root-permissions to view otherwise hidden files. | ||
*/ | ||
public class SUPickerFragment extends FilePickerFragment { | ||
|
||
@Override | ||
protected boolean hasPermission(@NonNull File path) { | ||
// Return the combination of normal file permissions and SU permissions | ||
return super.hasPermission(path) & (!needSUPermission(path) | hasSUPermission()); | ||
} | ||
|
||
@Override | ||
protected void handlePermission(@NonNull File path) { | ||
// Only call super if we don't have normal file permissions | ||
if (!super.hasPermission(path)) { | ||
super.handlePermission(path); | ||
} | ||
// Only if we need SU permissions | ||
if (needSUPermission(path) && !hasSUPermission()) { | ||
handleSUPermission(); | ||
} | ||
} | ||
|
||
private boolean haveReadPermission(@NonNull File file) { | ||
List<String> result = | ||
Shell.SH.run("test -r " + file.getAbsolutePath() + " && echo \"rootsuccess\""); | ||
return result != null && !result.isEmpty() && "rootsuccess".equals(result.get(0)); | ||
} | ||
|
||
private boolean needSUPermission(@NonNull File path) { | ||
return !haveReadPermission(path); | ||
} | ||
|
||
private boolean isSUAvailable() { | ||
return Shell.SU.available(); | ||
} | ||
|
||
private boolean hasSUPermission() { | ||
if (isSUAvailable()) { | ||
List<String> result = Shell.SU.run("ls -l /"); | ||
if (result != null && !result.isEmpty()) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private void handleSUPermission() { | ||
if (isSUAvailable()) { | ||
// request | ||
String suVersion = Shell.SU.version(false); | ||
String suVersionInternal = Shell.SU.version(true); | ||
Log.d("libsuperuser: ", "suVersion:"+suVersion+" suVersionInternal:"+suVersionInternal); | ||
} else { | ||
// Notify that no root access available | ||
SUErrorFragment.showDialog(getFragmentManager()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters