48 private static final String TAG =
"HomeActivity";
49 private static final String PARAM_SEARCH_QUERY =
"search_query";
51 private HomeBinding binding;
53 private BookmarkListAdapter bookmarkListAdapter;
54 private MenuItem searchMenuItem;
55 private SearchView searchView;
57 private ExternalDisplayManager externalDisplayManager;
59 @Override
public void onCreate(Bundle savedInstanceState)
61 super.onCreate(savedInstanceState);
62 binding = HomeBinding.inflate(getLayoutInflater());
63 setContentView(binding.getRoot());
65 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
66 checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) !=
67 PackageManager.PERMISSION_GRANTED)
69 requestPermissions(
new String[] { Manifest.permission.POST_NOTIFICATIONS }, 0);
72 externalDisplayManager =
new ExternalDisplayManager(
this);
74 long heapSize = Runtime.getRuntime().maxMemory();
75 Log.i(TAG,
"Max HeapSize: " + heapSize);
76 Log.i(TAG,
"App data folder: " + getFilesDir().toString());
79 Intent caller = getIntent();
80 Uri callParameter = caller.getData();
82 if (Intent.ACTION_VIEW.equals(caller.getAction()) && callParameter !=
null)
84 String refStr = ConnectionReference.getFileReference(callParameter.toString());
85 Bundle bundle =
new Bundle();
88 Intent bookmarkIntent =
90 bookmarkIntent.putExtras(bundle);
91 startActivity(bookmarkIntent);
94 viewModel =
new ViewModelProvider(
this).get(
HomeViewModel.class);
96 bookmarkListAdapter =
new BookmarkListAdapter();
97 bookmarkListAdapter.setCallbacks(
new BookmarkListAdapter.Callbacks() {
98 @Override public void onItemClick(String refStr)
100 if (ConnectionReference.isBookmarkReference(refStr) ||
101 ConnectionReference.isHostnameReference(refStr))
103 externalDisplayManager.launchSessionWithDisplayPicker(refStr);
105 if (searchView != null)
107 searchView.setQuery(
"", false);
108 searchView.setIconified(true);
110 if (searchMenuItem != null)
112 searchMenuItem.collapseActionView();
114 viewModel.loadBookmarks(
"");
118 @Override
public void onDelete(
long id)
120 viewModel.deleteBookmark(
id);
123 @Override
public void onExport(BookmarkBase bookmark)
125 shareRdpFile(bookmark);
129 binding.recyclerViewBookmarks.setLayoutManager(
new LinearLayoutManager(
this));
130 binding.recyclerViewBookmarks.setAdapter(bookmarkListAdapter);
132 viewModel.getBookmarks().observe(
this,
133 bookmarks -> bookmarkListAdapter.setItems(bookmarks));
136 if (savedInstanceState !=
null)
138 String query = savedInstanceState.getString(PARAM_SEARCH_QUERY);
139 if (query !=
null && !query.isEmpty() && viewModel.getCurrentQuery().isEmpty())
141 viewModel.loadBookmarks(query);
145 getOnBackPressedDispatcher().addCallback(
this,
new OnBackPressedCallback(
true) {
146 @Override
public void handleOnBackPressed()
151 .setTitle(R.string.dlg_title_exit)
152 .setMessage(R.string.dlg_msg_exit)
153 .setPositiveButton(R.string.yes, (dialog, which) -> finish())
154 .setNegativeButton(R.string.no, (dialog, which) -> dialog.dismiss())
165 @Override
protected void onResume()
168 Log.v(TAG,
"HomeActivity.onResume");
169 viewModel.loadBookmarks(viewModel.getCurrentQuery());
172 @Override
protected void onSaveInstanceState(Bundle outState)
174 super.onSaveInstanceState(outState);
175 outState.putString(PARAM_SEARCH_QUERY, viewModel.getCurrentQuery());
178 @Override
public boolean onCreateOptionsMenu(Menu menu)
180 MenuInflater inflater = getMenuInflater();
181 inflater.inflate(R.menu.home_menu, menu);
182 setupSearchView(menu);
186 @Override
public boolean onOptionsItemSelected(MenuItem item)
190 int itemId = item.getItemId();
191 if (itemId == R.id.newBookmark)
193 Intent bookmarkIntent =
new Intent(
this, BookmarkActivity.class);
194 startActivity(bookmarkIntent);
196 else if (itemId == R.id.appSettings)
198 Intent settingsIntent =
new Intent(
this, ApplicationSettingsActivity.class);
199 startActivity(settingsIntent);
201 else if (itemId == R.id.help)
203 Intent helpIntent =
new Intent(
this, HelpActivity.class);
204 startActivity(helpIntent);
206 else if (itemId == R.id.about)
208 Intent aboutIntent =
new Intent(
this, AboutActivity.class);
209 startActivity(aboutIntent);
215 private void shareRdpFile(BookmarkBase bookmark)
217 String filename = bookmark.getLabel().replaceAll(
"[^\\w. -]",
"_") +
".rdp";
218 File file =
new File(getCacheDir(), filename);
219 try (FileOutputStream out =
new FileOutputStream(file))
221 out.write(RDPFileHelper.toRdpString(bookmark).getBytes(StandardCharsets.UTF_8));
223 catch (IOException e)
225 Log.e(TAG,
"Failed to write RDP file for sharing", e);
226 Toast.makeText(
this, R.string.export_failed, Toast.LENGTH_SHORT).show();
229 Uri uri = FileProvider.getUriForFile(
this,
"com.freerdp.afreerdp.fileprovider", file);
230 Intent share =
new Intent(Intent.ACTION_SEND);
231 share.setType(
"application/x-rdp");
232 share.putExtra(Intent.EXTRA_STREAM, uri);
233 share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
234 startActivity(Intent.createChooser(share, bookmark.getLabel()));
237 private void setupSearchView(Menu menu)
239 searchMenuItem = menu.findItem(R.id.action_search);
240 if (searchMenuItem !=
null)
242 searchView = (SearchView)searchMenuItem.getActionView();
244 String currentQuery = viewModel.getCurrentQuery();
245 if (currentQuery !=
null && !currentQuery.isEmpty())
247 searchMenuItem.expandActionView();
248 searchView.setQuery(currentQuery,
false);
249 searchView.clearFocus();
252 searchView.setOnQueryTextListener(
new SearchView.OnQueryTextListener() {
253 @Override
public boolean onQueryTextSubmit(String query)
258 @Override
public boolean onQueryTextChange(String s)
260 viewModel.loadBookmarks(s);