summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/FileUtils.java
blob: 87507363fc90801fcd157bde299611aea5c85df7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.mapbox.mapboxsdk.utils;

import android.os.AsyncTask;
import android.support.annotation.NonNull;

import com.mapbox.mapboxsdk.log.Logger;

import java.io.File;

public class FileUtils {

  private static final String TAG = "Mbgl-FileUtils";

  /**
   * Task checking whether app's process can read a file.
   * <p>
   * The callback reference is <b>strongly kept</b> throughout the process,
   * so it needs to be wrapped in a weak reference or released on the client side if necessary.
   */
  public static class CheckFileReadPermissionTask extends AsyncTask<File, Void, Boolean> {
    @NonNull
    private final OnCheckFileReadPermissionListener listener;

    public CheckFileReadPermissionTask(@NonNull OnCheckFileReadPermissionListener listener) {
      this.listener = listener;
    }

    @Override
    protected Boolean doInBackground(File... files) {
      try {
        return files[0].canRead();
      } catch (Exception ex) {
        return false;
      }
    }

    @Override
    protected void onCancelled() {
      listener.onError();
    }

    @Override
    protected void onPostExecute(Boolean result) {
      if (result) {
        listener.onReadPermissionGranted();
      } else {
        listener.onError();
      }
    }
  }

  /**
   * Interface definition for a callback invoked when checking file's read permissions.
   */
  public interface OnCheckFileReadPermissionListener {

    /**
     * Invoked when app's process has a permission to read a file.
     */
    void onReadPermissionGranted();

    /**
     * Invoked when app's process doesn't have a permission to read a file or an error occurs.
     */
    void onError();
  }

  /**
   * Task checking whether app's process can write to a file.
   * <p>
   * The callback reference is <b>strongly kept</b> throughout the process,
   * so it needs to be wrapped in a weak reference or released on the client side if necessary.
   */
  public static class CheckFileWritePermissionTask extends AsyncTask<File, Void, Boolean> {
    @NonNull
    private final OnCheckFileWritePermissionListener listener;

    public CheckFileWritePermissionTask(@NonNull OnCheckFileWritePermissionListener listener) {
      this.listener = listener;
    }

    @Override
    protected Boolean doInBackground(File... files) {
      try {
        return files[0].canWrite();
      } catch (Exception ex) {
        return false;
      }
    }

    @Override
    protected void onCancelled() {
      listener.onError();
    }

    @Override
    protected void onPostExecute(Boolean result) {
      if (result) {
        listener.onWritePermissionGranted();
      } else {
        listener.onError();
      }
    }
  }

  /**
   * Interface definition for a callback invoked when checking file's write permissions.
   */
  public interface OnCheckFileWritePermissionListener {

    /**
     * Invoked when app's process has a permission to write to a file.
     */
    void onWritePermissionGranted();

    /**
     * Invoked when app's process doesn't have a permission to write to a file or an error occurs.
     */
    void onError();
  }

  /**
   * Deletes a file asynchronously in a separate thread.
   *
   * @param path the path of the file that should be deleted
   */
  public static void deleteFile(@NonNull final String path) {
    // Delete the file in a separate thread to avoid affecting the UI
    new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          File file = new File(path);
          if (file.exists()) {
            if (file.delete()) {
              Logger.d(TAG, "File deleted to save space: " + path);
            } else {
              Logger.e(TAG, "Failed to delete file: " + path);
            }
          }
        } catch (Exception exception) {
          Logger.e(TAG, "Failed to delete file: ", exception);
        }
      }
    }).start();
  }
}