wxr
2020-06-15 b8e94316e41eba72d927d5ca7d931b26139ee8ff
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/*****************************************************************************
 * Util.java
 *****************************************************************************
 * Copyright © 2011-2014 VLC authors and VideoLAN
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
 
package org.videolan.vlc;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import java.util.StringTokenizer;
 
import org.json.JSONArray;
import org.json.JSONException;
import org.videolan.libvlc.LibVLC;
import org.videolan.libvlc.LibVlcException;
import org.videolan.libvlc.LibVlcUtil;
import org.videolan.libvlc.Media;
 
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.sqlite.SQLiteFullException;
import android.graphics.Bitmap;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.WindowManager;
import android.widget.Toast;
 
public class Util
{
    public final static String TAG = "VLC/Util";
    private final static boolean hasNavBar;
    /** A set of utility functions for the VLC application */
 
    static
    {
        HashSet<String> devicesWithoutNavBar = new HashSet<String>();
        devicesWithoutNavBar.add("HTC One V");
        devicesWithoutNavBar.add("HTC One S");
        devicesWithoutNavBar.add("HTC One X");
        devicesWithoutNavBar.add("HTC One XL");
        hasNavBar = LibVlcUtil.isICSOrLater() && !devicesWithoutNavBar.contains(android.os.Build.MODEL);
    }
 
    public static LibVLC getLibVlcInstance(Context context) throws LibVlcException
    {
        LibVLC instance = LibVLC.getExistingInstance();
        if (instance == null)
        {
            Thread.setDefaultUncaughtExceptionHandler(new VlcCrashHandler());
 
            instance = LibVLC.getInstance();
            //Context context = VLCApplication.getAppContext();
            SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
            updateLibVlcSettings(pref);
            instance.init(context);
        }
        return instance;
    }
 
    public static float[] getFloatArray(SharedPreferences pref, String key)
    {
        float[] array = null;
        String s = pref.getString(key, null);
        if (s != null)
        {
            try
            {
                JSONArray json = new JSONArray(s);
                array = new float[json.length()];
                for (int i = 0; i < array.length; i++)
                    array[i] = (float) json.getDouble(i);
            }
            catch (JSONException e)
            {
                e.printStackTrace();
            }
        }
        return array;
    }
 
    public static void putFloatArray(Editor editor, String key, float[] array)
    {
        try
        {
            JSONArray json = new JSONArray();
            for (float f : array)
                json.put(f);
            editor.putString("equalizer_values", json.toString());
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
    }
 
    public static void updateLibVlcSettings(SharedPreferences pref)
    {
        LibVLC instance = LibVLC.getExistingInstance();
        if (instance == null)
            return;
 
        instance.setSubtitlesEncoding(pref.getString("subtitle_text_encoding", ""));
        instance.setTimeStretching(pref.getBoolean("enable_time_stretching_audio", false));
        instance.setFrameSkip(pref.getBoolean("enable_frame_skip", false));
        instance.setChroma(pref.getString("chroma_format", ""));
        instance.setVerboseMode(pref.getBoolean("enable_verbose_mode", true));
 
        if (pref.getBoolean("equalizer_enabled", false))
            instance.setEqualizer(getFloatArray(pref, "equalizer_values"));
 
        int aout;
        try
        {
            aout = Integer.parseInt(pref.getString("aout", "-1"));
        }
        catch (NumberFormatException nfe)
        {
            aout = -1;
        }
        int vout;
        try
        {
            vout = Integer.parseInt(pref.getString("vout", "-1"));
        }
        catch (NumberFormatException nfe)
        {
            vout = -1;
        }
        int deblocking;
        try
        {
            deblocking = Integer.parseInt(pref.getString("deblocking", "-1"));
        }
        catch (NumberFormatException nfe)
        {
            deblocking = -1;
        }
        int hardwareAcceleration;
        try
        {
            hardwareAcceleration = Integer.parseInt(pref.getString("hardware_acceleration", "-1"));
        }
        catch (NumberFormatException nfe)
        {
            hardwareAcceleration = -1;
        }
        int networkCaching = pref.getInt("network_caching_value", 0);
        if (networkCaching > 60000)
            networkCaching = 60000;
        else if (networkCaching < 0)
            networkCaching = 0;
        instance.setAout(aout);
        instance.setVout(vout);
        instance.setDeblocking(deblocking);
        instance.setNetworkCaching(networkCaching);
        instance.setHardwareAcceleration(hardwareAcceleration);
    }
 
    /** Print an on-screen message to alert the user */
    public static void toaster(Context context, int stringId, int duration)
    {
        Toast.makeText(context, stringId, duration).show();
    }
 
    public static void toaster(Context context, int stringId)
    {
        toaster(context, stringId, Toast.LENGTH_SHORT);
    }
 
    public static String stripTrailingSlash(String s)
    {
        if (s.endsWith("/") && s.length() > 1)
            return s.substring(0, s.length() - 1);
        return s;
    }
 
    public static String readAsset(String assetName, String defaultS)
    {
        try
        {
            InputStream is = VLCApplication.getAppResources().getAssets().open(assetName);
            BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF8"));
            StringBuilder sb = new StringBuilder();
            String line = r.readLine();
            if (line != null)
            {
                sb.append(line);
                line = r.readLine();
                while (line != null)
                {
                    sb.append('\n');
                    sb.append(line);
                    line = r.readLine();
                }
            }
            return sb.toString();
        }
        catch (IOException e)
        {
            return defaultS;
        }
    }
 
    /**
     * Writes the current app logcat to a file.
     * 
     * @param filename
     *            The filename to save it as
     * @throws IOException
     */
    public static void writeLogcat(String filename) throws IOException
    {
        String[] args =
        { "logcat", "-v", "time", "-d" };
 
        Process process = Runtime.getRuntime().exec(args);
        InputStreamReader input = new InputStreamReader(process.getInputStream());
        OutputStreamWriter output = new OutputStreamWriter(new FileOutputStream(filename));
        BufferedReader br = new BufferedReader(input);
        BufferedWriter bw = new BufferedWriter(output);
        String line;
 
        while ((line = br.readLine()) != null)
        {
            bw.write(line);
            bw.newLine();
        }
 
        bw.close();
        output.close();
        br.close();
        input.close();
    }
 
    /**
     * Convert time to a string
     * 
     * @param millis
     *            e.g.time/length from file
     * @return formated string (hh:)mm:ss
     */
    public static String millisToString(long millis)
    {
        return millisToString(millis, false);
    }
 
    /**
     * Convert time to a string
     * 
     * @param millis
     *            e.g.time/length from file
     * @return formated string "[hh]h[mm]min" / "[mm]min[s]s"
     */
    public static String millisToText(long millis)
    {
        return millisToString(millis, true);
    }
 
    private static String millisToString(long millis, boolean text)
    {
        boolean negative = millis < 0;
        millis = java.lang.Math.abs(millis);
 
        millis /= 1000;
        int sec = (int) (millis % 60);
        millis /= 60;
        int min = (int) (millis % 60);
        millis /= 60;
        int hours = (int) millis;
 
        String time;
        DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.US);
        format.applyPattern("00");
        if (text)
        {
            if (millis > 0)
                time = (negative ? "-" : "") + hours + "h" + format.format(min) + "min";
            else if (min > 0)
                time = (negative ? "-" : "") + min + "min";
            else
                time = (negative ? "-" : "") + sec + "s";
        }
        else
        {
            if (millis > 0)
                time = (negative ? "-" : "") + hours + ":" + format.format(min) + ":" + format.format(sec);
            else
                time = (negative ? "-" : "") + min + ":" + format.format(sec);
        }
        return time;
    }
 
    public static Bitmap scaleDownBitmap(Context context, Bitmap bitmap, int width)
    {
        /*
         * This method can lead to OutOfMemoryError! If the source size is more
         * than twice the target size use the optimized version available in
         * AudioUtil::readCoverBitmap
         */
        if (bitmap != null)
        {
            final float densityMultiplier = context.getResources().getDisplayMetrics().density;
            int w = (int) (width * densityMultiplier);
            int h = (int) (w * bitmap.getHeight() / ((double) bitmap.getWidth()));
            bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);
        }
        return bitmap;
    }
 
    public static Bitmap cropBorders(Bitmap bitmap, int width, int height)
    {
        int top = 0;
        for (int i = 0; i < height / 2; i++)
        {
            int pixel1 = bitmap.getPixel(width / 2, i);
            int pixel2 = bitmap.getPixel(width / 2, height - i - 1);
            if ((pixel1 == 0 || pixel1 == -16777216) && (pixel2 == 0 || pixel2 == -16777216))
            {
                top = i;
            }
            else
            {
                break;
            }
        }
 
        int left = 0;
        for (int i = 0; i < width / 2; i++)
        {
            int pixel1 = bitmap.getPixel(i, height / 2);
            int pixel2 = bitmap.getPixel(width - i - 1, height / 2);
            if ((pixel1 == 0 || pixel1 == -16777216) && (pixel2 == 0 || pixel2 == -16777216))
            {
                left = i;
            }
            else
            {
                break;
            }
        }
 
        if (left >= width / 2 - 10 || top >= height / 2 - 10)
            return bitmap;
 
        // Cut off the transparency on the borders
        return Bitmap.createBitmap(bitmap, left, top, (width - (2 * left)), (height - (2 * top)));
    }
 
    public static Bitmap getPictureFromCache(Media media)
    {
        // mPicture is not null only if passed through
        // the ctor which is deprecated by now.
        Bitmap b = media.getPicture();
        if (b == null)
        {
            BitmapCache cache = BitmapCache.getInstance();
            Bitmap picture = cache.getBitmapFromMemCache(media.getLocation());
            if (picture == null)
            {
                /*
                 * Not in memcache: serving the file from the database and
                 * adding it to the memcache for later use.
                 */
                Context c = VLCApplication.getAppContext();
                picture = MediaDatabase.getInstance(c).getPicture(c, media.getLocation());
                cache.addBitmapToMemCache(media.getLocation(), picture);
            }
            return picture;
        }
        else
        {
            return b;
        }
    }
 
    public static void setPicture(Context context, Media m, Bitmap p)
    {
        Log.d(TAG, "Setting new picture for " + m.getTitle());
        try
        {
            MediaDatabase.getInstance(context).updateMedia(m.getLocation(), MediaDatabase.mediaColumn.MEDIA_PICTURE, p);
        }
        catch (SQLiteFullException e)
        {
            Log.d(TAG, "SQLiteFullException while setting picture");
        }
        m.setPictureParsed(true);
    }
 
    public static String getValue(String string, int defaultId)
    {
        return (string != null && string.length() > 0) ? string : VLCApplication.getAppContext().getString(defaultId);
    }
 
    public static int convertPxToDp(int px)
    {
        WindowManager wm = (WindowManager) VLCApplication.getAppContext().getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);
        float logicalDensity = metrics.density;
        int dp = Math.round(px / logicalDensity);
        return dp;
    }
 
    public static int convertDpToPx(int dp)
    {
        return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, VLCApplication.getAppResources()
                .getDisplayMetrics()));
    }
 
    public static boolean hasExternalStorage()
    {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }
 
    public static boolean hasNavBar()
    {
        return hasNavBar;
    }
 
    /**
     * hasCombBar test if device has Combined Bar : only for tablet with
     * Honeycomb or ICS
     */
    public static boolean hasCombBar()
    {
        return (!isPhone() && ((android.os.Build.VERSION.SDK_INT >= 11) && (android.os.Build.VERSION.SDK_INT <= 16)));
 
        // android.os.Build.VERSION_CODES.HONEYCOMB
        // android.os.Build.VERSION_CODES.JELLY_BEAN
    }
 
    public static boolean isPhone()
    {
        TelephonyManager manager = (TelephonyManager) VLCApplication.getAppContext().getSystemService(
                Context.TELEPHONY_SERVICE);
        if (manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
 
    public static String[] getStorageDirectories()
    {
        String[] dirs = null;
        BufferedReader bufReader = null;
        try
        {
            bufReader = new BufferedReader(new FileReader("/proc/mounts"));
            ArrayList<String> list = new ArrayList<String>();
            list.add(Environment.getExternalStorageDirectory().getPath());
            String line;
            while ((line = bufReader.readLine()) != null)
            {
                if (line.contains("vfat") || line.contains("exfat") || line.contains("/mnt")
                        || line.contains("/Removable"))
                {
                    StringTokenizer tokens = new StringTokenizer(line, " ");
                    String s = tokens.nextToken();
                    s = tokens.nextToken(); // Take the second token, i.e. mount
                                            // point
 
                    if (list.contains(s))
                        continue;
 
                    if (line.contains("/dev/block/vold"))
                    {
                        if (!line.startsWith("tmpfs") && !line.startsWith("/dev/mapper")
                                && !s.startsWith("/mnt/secure") && !s.startsWith("/mnt/shell")
                                && !s.startsWith("/mnt/asec") && !s.startsWith("/mnt/obb"))
                        {
                            list.add(s);
                        }
                    }
                }
            }
 
            dirs = new String[list.size()];
            for (int i = 0; i < list.size(); i++)
            {
                dirs[i] = list.get(i);
            }
        }
        catch (FileNotFoundException e)
        {
        }
        catch (IOException e)
        {
        }
        finally
        {
            if (bufReader != null)
            {
                try
                {
                    bufReader.close();
                }
                catch (IOException e)
                {
                }
            }
        }
        return dirs;
    }
 
    public static String[] getCustomDirectories()
    {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(VLCApplication.getAppContext());
        final String custom_paths = preferences.getString("custom_paths", "");
        if (custom_paths.equals(""))
            return new String[0];
        else
            return custom_paths.split(":");
    }
 
    public static String[] getMediaDirectories()
    {
        ArrayList<String> list = new ArrayList<String>();
        list.addAll(Arrays.asList(Util.getStorageDirectories()));
        list.addAll(Arrays.asList(Util.getCustomDirectories()));
        return list.toArray(new String[list.size()]);
    }
 
    public static void addCustomDirectory(String path)
    {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(VLCApplication.getAppContext());
 
        ArrayList<String> dirs = new ArrayList<String>(Arrays.asList(getCustomDirectories()));
        dirs.add(path);
        StringBuilder builder = new StringBuilder();
        builder.append(dirs.remove(0));
        for (String s : dirs)
        {
            builder.append(":");
            builder.append(s);
        }
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("custom_paths", builder.toString());
        editor.commit();
    }
 
    public static void removeCustomDirectory(String path)
    {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(VLCApplication.getAppContext());
        if (!preferences.getString("custom_paths", "").contains(path))
            return;
        ArrayList<String> dirs = new ArrayList<String>(Arrays.asList(preferences.getString("custom_paths", "").split(
                ":")));
        dirs.remove(path);
        String custom_path;
        if (dirs.size() > 0)
        {
            StringBuilder builder = new StringBuilder();
            builder.append(dirs.remove(0));
            for (String s : dirs)
            {
                builder.append(":");
                builder.append(s);
            }
            custom_path = builder.toString();
        }
        else
        { // don't do unneeded extra work
            custom_path = "";
        }
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("custom_paths", custom_path);
        editor.commit();
    }
 
    /**
     * Get the formatted current playback speed in the form of 1.00x
     */
    public static String formatRateString(float rate)
    {
        return String.format(java.util.Locale.US, "%.2fx", rate);
    }
 
    /**
     * equals() with two strings where either could be null
     */
    public static boolean nullEquals(String s1, String s2)
    {
        return (s1 == null ? s2 == null : s1.equals(s2));
    }
}