• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.nn.benchmark.util;
2 
3 import android.app.Activity;
4 import android.content.pm.PackageManager;
5 import android.os.Build;
6 import android.os.Bundle;
7 import android.os.Environment;
8 import android.util.Log;
9 
10 import java.io.File;
11 import java.io.IOException;
12 
13 /**
14  * Helper class for testing and requesting WRITE_EXTERNAL_STORAGE permission
15  *
16  * If run sucessfuly, it will create /sdcard/mlts_write_external_storage file.
17  */
18 public class TestExternalStorageActivity extends Activity {
19     private static final String TAG = TestExternalStorageActivity.class.getSimpleName();
20 
21     @Override
onCreate(Bundle savedInstanceState)22     protected void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24 
25         try {
26             if (testWriteExternalStorage(this, true)) {
27                 try {
28                     new File(Environment.getExternalStorageDirectory(),
29                             "mlts_write_external_storage").createNewFile();
30                 } catch (IOException e) {
31                     Log.e(TAG, "Failed to create a file", e);
32                     throw new IllegalStateException("Failed to write to external storage", e);
33                 }
34             }
35         } finally {
36             finish();
37         }
38     }
39 
testWriteExternalStorage(Activity activity, boolean request)40     public static boolean testWriteExternalStorage(Activity activity, boolean request) {
41         if (Build.VERSION.SDK_INT >= 23) {
42             if (activity.checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
43                     == PackageManager.PERMISSION_GRANTED) {
44                 return true;
45             } else {
46                 if (request) {
47                     activity.requestPermissions(
48                             new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
49                 }
50                 return false;
51             }
52         }
53         return true;
54     }
55 }
56