JLChen
2021-01-04 ed86fe7ed952bb2151aac8841134246bbde152c9
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * Copyright (c) 2010-2019 Belledonne Communications SARL.
 *
 * This file is part of linphone-android
 * (see https://www.linphone.org).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.linphone.utils;
 
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
import android.text.TextUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import org.linphone.LinphoneManager;
import org.linphone.core.Address;
import org.linphone.core.Friend;
import org.linphone.core.FriendList;
import org.linphone.core.tools.Log;
 
public class FileUtils {
    public static String getNameFromFilePath(String filePath) {
        if (filePath == null) return null;
 
        String name = filePath;
        int i = filePath.lastIndexOf('/');
        if (i > 0) {
            name = filePath.substring(i + 1);
        }
        return name;
    }
 
    public static String getExtensionFromFileName(String fileName) {
        if (fileName == null) return null;
 
        String extension = null;
        int i = fileName.lastIndexOf('.');
        if (i > 0) {
            extension = fileName.substring(i + 1);
        }
        return extension;
    }
 
    public static Boolean isExtensionImage(String path) {
        String extension = getExtensionFromFileName(path);
        if (extension != null) extension = extension.toLowerCase();
        return (extension != null && extension.matches("(png|jpg|jpeg|bmp|gif)"));
    }
 
    public static String getFilePath(final Context context, final Uri uri) {
        if (uri == null) return null;
 
        String result = null;
        String name = getNameFromUri(uri, context);
 
        try {
            File localFile = createFile(context, name);
            InputStream remoteFile = context.getContentResolver().openInputStream(uri);
            Log.i(
                    "[File Utils] Trying to copy file from "
                            + uri.toString()
                            + " to local file "
                            + localFile.getAbsolutePath());
 
            if (copyToFile(remoteFile, localFile)) {
                Log.i("[File Utils] Copy successful");
                result = localFile.getAbsolutePath();
            } else {
                Log.e("[File Utils] Copy failed");
            }
 
            remoteFile.close();
        } catch (IOException e) {
            Log.e("[File Utils] getFilePath exception: ", e);
        }
 
        return result;
    }
 
    private static String getNameFromUri(Uri uri, Context context) {
        String name = null;
        if (uri != null) {
            if (uri.getScheme().equals("content")) {
                Cursor returnCursor =
                        context.getContentResolver().query(uri, null, null, null, null);
                if (returnCursor != null) {
                    returnCursor.moveToFirst();
                    int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                    name = returnCursor.getString(nameIndex);
                    returnCursor.close();
                }
            } else if (uri.getScheme().equals("file")) {
                name = uri.getLastPathSegment();
            }
        }
        return name;
    }
 
    /**
     * Copy data from a source stream to destFile. Return true if succeed, return false if failed.
     */
    private static boolean copyToFile(InputStream inputStream, File destFile) {
        if (inputStream == null || destFile == null) return false;
        try {
            try (OutputStream out = new FileOutputStream(destFile)) {
                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) >= 0) {
                    out.write(buffer, 0, bytesRead);
                }
            }
            return true;
        } catch (IOException e) {
            Log.e("[File Utils] copyToFile exception: " + e);
        }
        return false;
    }
 
    private static File createFile(Context context, String fileName) {
        if (fileName == null) return null;
        if (TextUtils.isEmpty(fileName)) fileName = getStartDate();
 
        if (!fileName.contains(".")) {
            fileName = fileName + ".unknown";
        }
 
        final File root;
        root = context.getExternalCacheDir();
 
        if (root != null && !root.exists()) {
            boolean result = root.mkdirs();
            if (!result) {
                Log.e("[File Utils] Couldn't create directory " + root.getAbsolutePath());
            }
        }
        return new File(root, fileName);
    }
 
    public static Uri getCVSPathFromLookupUri(String content) {
        if (content == null) return null;
 
        String contactId = getNameFromFilePath(content);
        FriendList[] friendList = LinphoneManager.getCore().getFriendsLists();
        for (FriendList list : friendList) {
            for (Friend friend : list.getFriends()) {
                if (friend.getRefKey().equals(contactId)) {
                    String contactVcard = friend.getVcard().asVcard4String();
                    return createCvsFromString(contactVcard);
                }
            }
        }
        return null;
    }
 
    public static String getRealPathFromURI(Context context, Uri contentUri) {
        String[] proj = {MediaStore.Images.Media.DATA};
        Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            String result = cursor.getString(column_index);
            cursor.close();
            return result;
        }
        return null;
    }
 
    public static void deleteFile(String filePath) {
        if (filePath == null || filePath.isEmpty()) return;
        File file = new File(filePath);
        if (file.exists()) {
            try {
                if (file.delete()) {
                    Log.i("[File Utils] File deleted: ", filePath);
                } else {
                    Log.e("[File Utils] Can't delete ", filePath);
                }
            } catch (Exception e) {
                Log.e("[File Utils] Can't delete ", filePath, ", exception: ", e);
            }
        } else {
            Log.e("[File Utils] File ", filePath, " doesn't exists");
        }
    }
 
    public static String getStorageDirectory(Context mContext) {
        File path = null;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            Log.w("[File Utils] External storage is mounted");
            String directory = Environment.DIRECTORY_DOWNLOADS;
            path = mContext.getExternalFilesDir(directory);
        }
 
        if (path == null) {
            Log.w("[File Utils] Couldn't get external storage path, using internal");
            path = mContext.getFilesDir();
        }
 
        return path.getAbsolutePath();
    }
 
    public static String getRecordingsDirectory(Context mContext) {
        return getStorageDirectory(mContext);
    }
 
    @SuppressLint("SimpleDateFormat")
    public static String getCallRecordingFilename(Context context, Address address) {
        String fileName = getRecordingsDirectory(context) + "/";
 
        String name =
                address.getDisplayName() == null ? address.getUsername() : address.getDisplayName();
        fileName += name + "_";
 
        DateFormat format = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss");
        fileName += format.format(new Date()) + ".mkv";
 
        return fileName;
    }
 
    private static Uri createCvsFromString(String vcardString) {
        String contactName = getContactNameFromVcard(vcardString);
        File vcfFile = new File(Environment.getExternalStorageDirectory(), contactName + ".cvs");
        try {
            FileWriter fw = new FileWriter(vcfFile);
            fw.write(vcardString);
            fw.close();
            return Uri.fromFile(vcfFile);
        } catch (IOException e) {
            Log.e("[File Utils] createCVSFromString exception: " + e);
        }
        return null;
    }
 
    private static String getContactNameFromVcard(String vcard) {
        if (vcard != null) {
            String contactName = vcard.substring(vcard.indexOf("FN:") + 3);
            contactName = contactName.substring(0, contactName.indexOf("\n") - 1);
            contactName = contactName.replace(";", "");
            contactName = contactName.replace(" ", "");
            return contactName;
        }
        return null;
    }
 
    private static String getStartDate() {
        try {
            return new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ROOT).format(new Date());
        } catch (RuntimeException e) {
            return new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
        }
    }
 
    public static String getMimeFromFile(String path) {
        if (isExtensionImage(path)) {
            return "image/" + getExtensionFromFileName(path);
        }
        return "file/" + getExtensionFromFileName(path);
    }
}