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
70 binding.recyclerViewShortcuts.setLayoutManager(new LinearLayoutManager(this));
71 binding.recyclerViewShortcuts.setAdapter(adapter);
72
73 viewModel.getBookmarks().observe(this, adapter::setItems);
74
75 viewModel.loadBookmarks();
76 }
77
78 @Override public boolean onSupportNavigateUp()
79 {
80 finish();
81 return true;
82 }
83
84 private static BookmarkBase findBookmark(BookmarkListAdapter adapter, String refStr)
85 {
86 for (BookmarkBase b : adapter.getItems())
87 {
88 if (ConnectionReference.getBookmarkReference(b.getId()).equals(refStr))
89 return b;
90 }
91 return null;
92 }
93
94 private void setupShortcut(String strRef, String defaultLabel)
95 {
96 final EditText input = new EditText(this);
97 input.setText(defaultLabel);
98
99 new AlertDialog.Builder(this)
100 .setTitle(R.string.dlg_title_create_shortcut)
101 .setMessage(R.string.dlg_msg_create_shortcut)
102 .setView(input)
103 .setPositiveButton(
104 android.R.string.ok,
105 (dialog, which) -> {
106 String label = input.getText().toString();
107 if (label.isEmpty())
108 label = defaultLabel;
109
110 Intent shortcutIntent = new Intent(Intent.ACTION_VIEW);
111 shortcutIntent.setClassName(this,
112 SessionRequestHandlerActivity.class.getName());
113 shortcutIntent.setData(Uri.parse(strRef));
114
115 ShortcutInfoCompat shortcutInfo =
116 new ShortcutInfoCompat.Builder(this, "shortcut_" + strRef.hashCode())
117 .setShortLabel(label)
118 .setIcon(IconCompat.createWithResource(
119 this, R.drawable.icon_launcher_freerdp))
120 .setIntent(shortcutIntent)
121 .build();
122
123 setResult(RESULT_OK,
124 ShortcutManagerCompat.createShortcutResultIntent(this, shortcutInfo));
125 finish();
126 })
127 .setNegativeButton(android.R.string.cancel, (dialog, which) -> dialog.dismiss())
128 .show();
129 }
130}