Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NewsDetailImageDialogFragment.java « owncloudnewsreader « luhmer « de « java « main « src « News-Android-App - github.com/nextcloud/news-android.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f7f000596bf1eba1682493488b772f6f5fd03f5a (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
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
package de.luhmer.owncloudnewsreader;

import android.Manifest;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import net.rdrei.android.dirchooser.DirectoryChooserConfig;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import androidx.core.app.ActivityCompat;
import androidx.core.content.PermissionChecker;
import androidx.fragment.app.DialogFragment;
import de.luhmer.owncloudnewsreader.helper.NewsFileUtils;
import de.luhmer.owncloudnewsreader.notification.NextcloudNotificationManager;

import static androidx.core.content.PermissionChecker.checkSelfPermission;

public class NewsDetailImageDialogFragment extends DialogFragment {

    private static final int REQUEST_DIRECTORY = 0;
    public enum TYPE { IMAGE, URL }
    private static final String TAG = NewsDetailImageDialogFragment.class.getCanonicalName();

    private int mDialogIcon;
    private String mDialogTitle;
    private String mDialogText;
    private URL mImageUrl;
    private TYPE mDialogType;

    private long downloadID;
    private DownloadManager downloadManager;
    private BroadcastReceiver downloadCompleteReceiver;

    private LinkedHashMap<String, MenuAction> mMenuItems;

    static NewsDetailImageDialogFragment newInstanceImage(String dialogTitle, Integer titleIcon, String dialogText, URL imageUrl) {
        NewsDetailImageDialogFragment f = new NewsDetailImageDialogFragment();

        if(titleIcon == null) {
            titleIcon = android.R.drawable.ic_menu_info_details;
        }

        Bundle args = new Bundle();
        args.putSerializable("dialogType", TYPE.IMAGE);
        args.putInt("titleIcon", titleIcon);
        args.putString("title", dialogTitle);
        args.putString("text", dialogText);
        args.putSerializable("imageUrl", imageUrl);
        f.setArguments(args);
        return f;
    }

    protected static NewsDetailImageDialogFragment newInstanceUrl(String dialogTitle, String dialogText) {
        NewsDetailImageDialogFragment f = new NewsDetailImageDialogFragment();

        Bundle args = new Bundle();
        args.putSerializable("dialogType", TYPE.URL);
        args.putInt("titleIcon", android.R.drawable.ic_menu_info_details);
        args.putString("title", dialogTitle);
        args.putString("text", dialogText);
        f.setArguments(args);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDialogIcon = getArguments().getInt("titleIcon");
        mDialogTitle = getArguments().getString("title");
        mDialogText = getArguments().getString("text");
        mImageUrl = (URL) getArguments().getSerializable("imageUrl");
        mDialogType = (TYPE) getArguments().getSerializable("dialogType");

        mMenuItems = new LinkedHashMap<>();

        //Build the menu
        switch(mDialogType) {
            case IMAGE:
                if(mImageUrl.toString().startsWith("http")) { //Only allow download for http[s] images (prevent download of cached images)
                    mMenuItems.put(getString(R.string.action_img_download), new MenuActionLongClick() {
                        @Override
                        public void execute() {
                            if (haveStoragePermission()) {
                                downloadImage(mImageUrl);
                            }
                        }

                        public void executeLongClick() {
                            changeDownloadDir();
                        }
                    });
                    mMenuItems.put(getString(R.string.action_img_open), new MenuAction() {
                        @Override
                        public void execute() {
                            openLinkInBrowser(mImageUrl);
                        }
                    });
                    mMenuItems.put(getString(R.string.action_img_sharelink), new MenuAction() {
                        @Override
                        public void execute() {
                            shareImage();
                        }
                    });
                    mMenuItems.put(getString(R.string.action_img_copylink), new MenuAction() {
                        @Override
                        public void execute() {
                            copyToClipboard(mDialogTitle, mImageUrl.toString());
                        }
                    });
                } else if (mImageUrl.toString().startsWith("file:///")) {
                    mMenuItems.put(getString(R.string.action_img_download), new MenuActionLongClick() {
                        @Override
                        public void execute() {
                            if (haveStoragePermission()) {
                                storeCachedImage(mImageUrl.getPath());
                            }
                        }

                        public void executeLongClick() {
                            changeDownloadDir();
                        }
                    });
                } else {
                    mDialogTitle = "Unknown Type";
                    mDialogText = "The URL type of image url: \"" + mImageUrl.toString() + "\" is unknown, please report this issue.";
                }
                break;
            case URL:
                mMenuItems.put(getString(R.string.action_link_open), new MenuAction() {
                    @Override
                    public void execute() {
                        try {
                            openLinkInBrowser(new URL(mDialogText));
                        } catch (MalformedURLException e) {
                            Toast.makeText(getActivity(), getString(R.string.error_invalid_url), Toast.LENGTH_SHORT).show();
                            e.printStackTrace();
                        }
                    }
                });
                mMenuItems.put(getString(R.string.action_link_share), new MenuAction() {
                    @Override
                    public void execute() {
                        shareLink();
                    }
                });
                mMenuItems.put(getString(R.string.action_link_copy), new MenuAction() {
                    @Override
                    public void execute() {
                        copyToClipboard(mDialogTitle, mDialogText);
                    }
                });
                break;
        }

        int style = DialogFragment.STYLE_NO_TITLE;
        int theme = R.style.FloatingDialog;
        setStyle(style, theme);
    }

    @Override
    public void onStart() {
        showDownloadShowcase();
        super.onStart();
    }

    private void showDownloadShowcase() {
        if(mMenuItems.containsKey(getActivity().getString(R.string.action_img_download))) {
            List<String> menuItemsList = new ArrayList<>(mMenuItems.keySet());
            int position = menuItemsList.indexOf(getActivity().getString(R.string.action_img_download));
            Log.v(TAG, "Position of Download Menu: " + position);
            /*
            // Bug in the Library.. ShowcaseView is rendered behind the DialogFragment //TODO check https://github.com/deano2390/MaterialShowcaseView/issues/51 for updates
            new MaterialShowcaseView.Builder(getActivity())
                    .setTarget(mListView /*.getChildAt(position) *//*)
                    .setDismissText("GOT IT")
                    .setContentText("Long press to change the target download directory")
                    .setDelay(300) // optional but starting animations immediately in onCreate can make them choppy
                    .singleUse("LONG_PRESS_DOWNLOAD_TARGET_DIR") // provide a unique ID used to ensure it is only shown once
                    .show();
            */
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_dialog_image, container, false);

        TextView tvTitle   = v.findViewById(R.id.ic_menu_title);
        TextView tvText    = v.findViewById(R.id.ic_menu_item_text);
        ImageView imgTitle = v.findViewById(R.id.ic_menu_gallery);

        tvTitle.setText(mDialogTitle);
        tvText.setText(mDialogText);
        imgTitle.setImageResource(mDialogIcon);

        if(mDialogType == TYPE.IMAGE) {
            registerImageDownloadReceiver();
            if(mDialogText.equals(mDialogTitle) || mDialogText.equals("")) {
                tvText.setVisibility(View.GONE);
            }
        }

        ListView mListView = (ListView) v.findViewById(R.id.ic_menu_item_list);
        List<String> menuItemsList = new ArrayList<>(mMenuItems.keySet());

        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(
                getActivity(),
                R.layout.fragment_dialog_listviewitem,
                menuItemsList);

        mListView.setAdapter(arrayAdapter);
        mListView.setLongClickable(true);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String key = arrayAdapter.getItem(i);
                MenuAction mAction = mMenuItems.get(key);
                mAction.execute();
            }
        });

        mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
                String key = arrayAdapter.getItem(position);
                try {
                    MenuActionLongClick mAction = (MenuActionLongClick) mMenuItems.get(key);
                    mAction.executeLongClick();
                } catch (ClassCastException e) {
                    return false;
                }
                return true;
            }
        });

        return v;
    }

    @Override
    public void onDestroyView() {
        unregisterImageDownloadReceiver();
        super.onDestroyView();
    }


    private void copyToClipboard(String label, String text) {
        ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Activity.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText(label, text);
        clipboard.setPrimaryClip(clip);
        Toast.makeText(getActivity(), getString(R.string.toast_copied_to_clipboard), Toast.LENGTH_SHORT).show();
        dismiss();
    }

    private void shareImage() {
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        sharingIntent.putExtra(Intent.EXTRA_SUBJECT, mDialogText);
        sharingIntent.putExtra(Intent.EXTRA_TEXT, mImageUrl.toString());
        startActivity(Intent.createChooser(sharingIntent, getString(R.string.intent_title_share)));
        dismiss();
    }

    private void shareLink() {
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        sharingIntent.putExtra(Intent.EXTRA_SUBJECT, mDialogTitle);
        sharingIntent.putExtra(Intent.EXTRA_TEXT, mDialogText);
        startActivity(Intent.createChooser(sharingIntent, getString(R.string.intent_title_share)));
        dismiss();
    }


    private void openLinkInBrowser(URL url) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url.toString()));
        startActivity(i);
        dismiss();
    }

    private void downloadImage(URL url) {
        Toast.makeText(getActivity().getApplicationContext(), getString(R.string.toast_img_download_wait), Toast.LENGTH_SHORT).show();

        if(isExternalStorageWritable()) {
            String filename = url.getFile().substring(url.getFile().lastIndexOf('/') + 1, url.getFile().length());
            downloadManager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url.toString()));
            request.setDestinationUri(getDownloadDir(filename));
            request.setTitle(getString(R.string.app_name) + " - " + getString(R.string.action_img_download));
            request.setDescription(filename);
            request.setVisibleInDownloadsUi(true);
            //request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
            //request.setAllowedOverRoaming(false);
            //request.setVisibleInDownloadsUi(false);
            //request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
            downloadID = downloadManager.enqueue(request);
            getDialog().hide();
        } else {
            Toast.makeText(getActivity().getApplicationContext(), getString(R.string.toast_img_notwriteable), Toast.LENGTH_LONG).show();
            dismiss();
        }
    }


    private void storeCachedImage(String path) {
        final String CHANNEL_ID = "Store cached Image";
        if(isExternalStorageWritable()) {
            String filename = path.substring(path.lastIndexOf('/') + 1, path.length());
            File dstPath = new File(getDownloadDir(filename).getPath());
            try {
                NewsFileUtils.copyFile(new FileInputStream(path), new FileOutputStream(dstPath));
            } catch (IOException e) {
                Toast.makeText(getActivity().getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }
            NextcloudNotificationManager.showNotificationSaveSingleCachedImageService(getActivity().getApplicationContext(), CHANNEL_ID, dstPath);
            getDialog().hide();
        } else {
            Toast.makeText(getActivity().getApplicationContext(), getString(R.string.toast_img_notwriteable), Toast.LENGTH_LONG).show();
            dismiss();
        }
    }

    public boolean haveStoragePermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PermissionChecker.PERMISSION_GRANTED) {
                Log.v("Permission error","You have permission");
                return true;
            } else {
                Log.e("Permission error","Asking for permission");
                ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                return false;
            }
        }
        else { //you dont need to worry about these stuff below api level 23
            Log.e("Permission error","You already have the permission");
            return true;
        }
    }


    private void changeDownloadDir() {
        final Intent chooserIntent = new Intent(getActivity(), DirectoryChooserActivity.class);
        final DirectoryChooserConfig config = DirectoryChooserConfig.builder()
                .initialDirectory(getActivity().getPreferences(Context.MODE_PRIVATE).getString("manualImageDownloadLocation", ""))
                .newDirectoryName("new folder")
                .allowNewDirectoryNameModification(true)
                .allowReadOnlyDirectory(false)
                .build();

        chooserIntent.putExtra(DirectoryChooserActivity.EXTRA_CONFIG, config);
        startActivityForResult(chooserIntent, REQUEST_DIRECTORY);
    }

    private void setNewDownloadDir(String path) {
        if(path.equals("")) {
            path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
        }
        SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString("manualImageDownloadLocation", path);
        editor.commit();
    }

    private Uri getDownloadDir(String filename) {
        SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
        String dir = sharedPref.getString("manualImageDownloadLocation", "");
        if(dir.equals("")) { //sharedPref has never been set
            setNewDownloadDir(""); //set to default public download dir
            return getDownloadDir(filename);
        }
        String tmp = "file://" +dir +"/" +filename;
        return Uri.parse(tmp);
    }


    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_DIRECTORY) {
            if (resultCode == DirectoryChooserActivity.RESULT_CODE_DIR_SELECTED) {
                String dir = data.getStringExtra(DirectoryChooserActivity.RESULT_SELECTED_DIR);
                setNewDownloadDir(dir);
            }
        }
    }

    private void unregisterImageDownloadReceiver() {
        if (downloadCompleteReceiver != null) {
            getActivity().unregisterReceiver(downloadCompleteReceiver);
            downloadCompleteReceiver = null;
        }
    }

    private void registerImageDownloadReceiver() {
        if(downloadCompleteReceiver != null) return;

        downloadCompleteReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                long refID = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
                if (downloadID == refID) {
                    DownloadManager.Query query = new DownloadManager.Query();
                    query.setFilterById(refID);
                    Cursor cursor = downloadManager.query(query);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
                    int status = cursor.getInt(columnIndex);
                    int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
                    int reason = cursor.getInt(columnReason);

                    switch (status) {
                        case DownloadManager.STATUS_SUCCESSFUL:
                            Toast.makeText(getActivity().getApplicationContext(), getString(R.string.toast_img_saved), Toast.LENGTH_LONG).show();

                            //String imagePath = downloadManager.getUriForDownloadedFile(refID).toString();

                            String downloadFileLocalUri = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                            File image = new File(Uri.parse(downloadFileLocalUri).getPath());

                            NextcloudNotificationManager.showNotificationDownloadSingleImageComplete(context, image);

                            if(isVisible()) {
                                dismiss();
                            }
                            break;
                        case DownloadManager.STATUS_FAILED:
                            Toast.makeText(getActivity().getApplicationContext(), getString(R.string.error_download_failed) + ": " + reason, Toast.LENGTH_LONG).show();
                            if(isVisible()) {
                                dismiss();
                            }
                            break;
                        default:
                            Log.e(TAG, "this should never happen! - unknown download status");
                    }
                }
            }
        };
        IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
        getActivity().registerReceiver(downloadCompleteReceiver, intentFilter);
    }

    public boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        return Environment.MEDIA_MOUNTED.equals(state);
    }


    interface MenuAction {
        void execute();
    }
    interface MenuActionLongClick extends MenuAction {
        void executeLongClick();
    }
}