FreeRDP
Loading...
Searching...
No Matches
ShortcutsActivity.java
1/*
2 Android Shortcut activity
3
4 Copyright 2013 Thincast Technologies GmbH, Author: Martin Fleisz
5
6 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7 If a copy of the MPL was not distributed with this file, You can obtain one at
8 http://mozilla.org/MPL/2.0/.
9*/
10
11package com.freerdp.freerdpcore.presentation;
12
13import android.content.Intent;
14import android.net.Uri;
15import android.os.Bundle;
16import android.widget.EditText;
17
18import androidx.appcompat.app.AlertDialog;
19import androidx.appcompat.app.AppCompatActivity;
20import androidx.core.content.pm.ShortcutInfoCompat;
21import androidx.core.content.pm.ShortcutManagerCompat;
22import androidx.core.graphics.drawable.IconCompat;
23import androidx.lifecycle.ViewModelProvider;
24import androidx.recyclerview.widget.LinearLayoutManager;
25
26import com.freerdp.freerdpcore.R;
27import com.freerdp.freerdpcore.databinding.ActivityShortcutsBinding;
28import com.freerdp.freerdpcore.domain.BookmarkBase;
29import com.freerdp.freerdpcore.domain.ConnectionReference;
30import com.freerdp.freerdpcore.services.SessionRequestHandlerActivity;
31import com.freerdp.freerdpcore.utils.BookmarkListAdapter;
32
33public class ShortcutsActivity extends AppCompatActivity
34{
35 @Override public void onCreate(Bundle savedInstanceState)
36 {
37 super.onCreate(savedInstanceState);
38
39 if (!Intent.ACTION_CREATE_SHORTCUT.equals(getIntent().getAction()))
40 {
41 finish();
42 return;
43 }
44
45 ActivityShortcutsBinding binding = ActivityShortcutsBinding.inflate(getLayoutInflater());
46 setContentView(binding.getRoot());
47
48 if (getSupportActionBar() != null)
49 getSupportActionBar().setDisplayHomeAsUpEnabled(true);
50
51 ShortcutsViewModel viewModel = new ViewModelProvider(this).get(ShortcutsViewModel.class);
52
53 BookmarkListAdapter adapter = new BookmarkListAdapter();
54 adapter.setActionsEnabled(false);
55 adapter.setCallbacks(new BookmarkListAdapter.Callbacks() {
56 @Override public void onItemClick(String refStr)
57 {
58 if (!ConnectionReference.isBookmarkReference(refStr))
59 return;
60 BookmarkBase bookmark = findBookmark(adapter, refStr);
61 String label = bookmark != null ? bookmark.getLabel() : refStr;
62 setupShortcut(refStr, label);
63 }
64
65 @Override public void onDelete(long id)
66 {
67 }
68
69 @Override public void onExport(BookmarkBase bookmark)
70 {
71 }
72 });
73
74 binding.recyclerViewShortcuts.setLayoutManager(new LinearLayoutManager(this));
75 binding.recyclerViewShortcuts.setAdapter(adapter);
76
77 viewModel.getBookmarks().observe(this, adapter::setItems);
78
79 viewModel.loadBookmarks();
80 }
81
82 @Override public boolean onSupportNavigateUp()
83 {
84 finish();
85 return true;
86 }
87
88 private static BookmarkBase findBookmark(BookmarkListAdapter adapter, String refStr)
89 {
90 for (BookmarkBase b : adapter.getItems())
91 {
92 if (ConnectionReference.getBookmarkReference(b.getId()).equals(refStr))
93 return b;
94 }
95 return null;
96 }
97
98 private void setupShortcut(String strRef, String defaultLabel)
99 {
100 final EditText input = new EditText(this);
101 input.setText(defaultLabel);
102
103 new AlertDialog.Builder(this)
104 .setTitle(R.string.dlg_title_create_shortcut)
105 .setMessage(R.string.dlg_msg_create_shortcut)
106 .setView(input)
107 .setPositiveButton(
108 android.R.string.ok,
109 (dialog, which) -> {
110 String label = input.getText().toString();
111 if (label.isEmpty())
112 label = defaultLabel;
113
114 Intent shortcutIntent = new Intent(Intent.ACTION_VIEW);
115 shortcutIntent.setClassName(this,
116 SessionRequestHandlerActivity.class.getName());
117 shortcutIntent.setData(Uri.parse(strRef));
118
119 ShortcutInfoCompat shortcutInfo =
120 new ShortcutInfoCompat.Builder(this, "shortcut_" + strRef.hashCode())
121 .setShortLabel(label)
122 .setIcon(IconCompat.createWithResource(this, R.mipmap.ic_launcher))
123 .setIntent(shortcutIntent)
124 .build();
125
126 setResult(RESULT_OK,
127 ShortcutManagerCompat.createShortcutResultIntent(this, shortcutInfo));
128 finish();
129 })
130 .setNegativeButton(android.R.string.cancel, (dialog, which) -> dialog.dismiss())
131 .show();
132 }
133}