FreeRDP
Loading...
Searching...
No Matches
HomeActivity.java
1/*
2 Main/Home 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 androidx.appcompat.app.AlertDialog;
14import android.content.Intent;
15import android.net.Uri;
16import android.os.Bundle;
17import androidx.appcompat.app.AppCompatActivity;
18import android.util.Log;
19import android.view.Menu;
20import android.view.MenuInflater;
21import android.view.MenuItem;
22
23import androidx.activity.OnBackPressedCallback;
24import androidx.appcompat.widget.SearchView;
25import androidx.lifecycle.ViewModelProvider;
26import androidx.recyclerview.widget.LinearLayoutManager;
27
28import com.freerdp.freerdpcore.R;
29import com.freerdp.freerdpcore.databinding.HomeBinding;
30import com.freerdp.freerdpcore.domain.ConnectionReference;
31import com.freerdp.freerdpcore.utils.BookmarkListAdapter;
32
33public class HomeActivity extends AppCompatActivity
34{
35 private static final String TAG = "HomeActivity";
36 private static final String PARAM_SEARCH_QUERY = "search_query";
37
38 private HomeBinding binding;
39 private HomeViewModel viewModel;
40 private BookmarkListAdapter bookmarkListAdapter;
41 private MenuItem searchMenuItem;
42 private SearchView searchView;
43
44 @Override public void onCreate(Bundle savedInstanceState)
45 {
46 super.onCreate(savedInstanceState);
47 binding = HomeBinding.inflate(getLayoutInflater());
48 setContentView(binding.getRoot());
49
50 long heapSize = Runtime.getRuntime().maxMemory();
51 Log.i(TAG, "Max HeapSize: " + heapSize);
52 Log.i(TAG, "App data folder: " + getFilesDir().toString());
53
54 // check for passed .rdp file and open it in a new bookmark
55 Intent caller = getIntent();
56 Uri callParameter = caller.getData();
57
58 if (Intent.ACTION_VIEW.equals(caller.getAction()) && callParameter != null)
59 {
60 String refStr = ConnectionReference.getFileReference(callParameter.getPath());
61 Bundle bundle = new Bundle();
62 bundle.putString(BookmarkActivity.PARAM_CONNECTION_REFERENCE, refStr);
63
64 Intent bookmarkIntent =
65 new Intent(this.getApplicationContext(), BookmarkActivity.class);
66 bookmarkIntent.putExtras(bundle);
67 startActivity(bookmarkIntent);
68 }
69
70 viewModel = new ViewModelProvider(this).get(HomeViewModel.class);
71
72 bookmarkListAdapter = new BookmarkListAdapter();
73 bookmarkListAdapter.setCallbacks(new BookmarkListAdapter.Callbacks() {
74 @Override public void onItemClick(String refStr)
75 {
76 if (ConnectionReference.isBookmarkReference(refStr) ||
77 ConnectionReference.isHostnameReference(refStr))
78 {
79 Bundle bundle = new Bundle();
80 bundle.putString(SessionActivity.PARAM_CONNECTION_REFERENCE, refStr);
81
82 Intent sessionIntent = new Intent(HomeActivity.this, SessionActivity.class);
83 sessionIntent.putExtras(bundle);
84 startActivity(sessionIntent);
85
86 if (searchView != null)
87 {
88 searchView.setQuery("", false);
89 searchView.setIconified(true);
90 }
91 if (searchMenuItem != null)
92 {
93 searchMenuItem.collapseActionView();
94 }
95 viewModel.loadBookmarks("");
96 }
97 }
98
99 @Override public void onDelete(long id)
100 {
101 viewModel.deleteBookmark(id);
102 }
103 });
104
105 binding.recyclerViewBookmarks.setLayoutManager(new LinearLayoutManager(this));
106 binding.recyclerViewBookmarks.setAdapter(bookmarkListAdapter);
107
108 viewModel.getBookmarks().observe(this,
109 bookmarks -> bookmarkListAdapter.setItems(bookmarks));
110
111 // restore search query after process death
112 if (savedInstanceState != null)
113 {
114 String query = savedInstanceState.getString(PARAM_SEARCH_QUERY);
115 if (query != null && !query.isEmpty() && viewModel.getCurrentQuery().isEmpty())
116 {
117 viewModel.loadBookmarks(query);
118 }
119 }
120
121 getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
122 @Override public void handleOnBackPressed()
123 {
124 if (ApplicationSettingsActivity.getAskOnExit(HomeActivity.this))
125 {
126 new AlertDialog.Builder(HomeActivity.this)
127 .setTitle(R.string.dlg_title_exit)
128 .setMessage(R.string.dlg_msg_exit)
129 .setPositiveButton(R.string.yes, (dialog, which) -> finish())
130 .setNegativeButton(R.string.no, (dialog, which) -> dialog.dismiss())
131 .show();
132 }
133 else
134 {
135 finish();
136 }
137 }
138 });
139 }
140
141 @Override protected void onResume()
142 {
143 super.onResume();
144 Log.v(TAG, "HomeActivity.onResume");
145 viewModel.loadBookmarks(viewModel.getCurrentQuery());
146 }
147
148 @Override protected void onSaveInstanceState(Bundle outState)
149 {
150 super.onSaveInstanceState(outState);
151 outState.putString(PARAM_SEARCH_QUERY, viewModel.getCurrentQuery());
152 }
153
154 @Override public boolean onCreateOptionsMenu(Menu menu)
155 {
156 MenuInflater inflater = getMenuInflater();
157 inflater.inflate(R.menu.home_menu, menu);
158 setupSearchView(menu);
159 return true;
160 }
161
162 @Override public boolean onOptionsItemSelected(MenuItem item)
163 {
164 // refer to http://tools.android.com/tips/non-constant-fields why we can't use switch/case
165 // here ..
166 int itemId = item.getItemId();
167 if (itemId == R.id.newBookmark)
168 {
169 Intent bookmarkIntent = new Intent(this, BookmarkActivity.class);
170 startActivity(bookmarkIntent);
171 }
172 else if (itemId == R.id.appSettings)
173 {
174 Intent settingsIntent = new Intent(this, ApplicationSettingsActivity.class);
175 startActivity(settingsIntent);
176 }
177 else if (itemId == R.id.help)
178 {
179 Intent helpIntent = new Intent(this, HelpActivity.class);
180 startActivity(helpIntent);
181 }
182 else if (itemId == R.id.about)
183 {
184 Intent aboutIntent = new Intent(this, AboutActivity.class);
185 startActivity(aboutIntent);
186 }
187
188 return true;
189 }
190
191 private void setupSearchView(Menu menu)
192 {
193 searchMenuItem = menu.findItem(R.id.action_search);
194 if (searchMenuItem != null)
195 {
196 searchView = (SearchView)searchMenuItem.getActionView();
197
198 String currentQuery = viewModel.getCurrentQuery();
199 if (currentQuery != null && !currentQuery.isEmpty())
200 {
201 searchMenuItem.expandActionView();
202 searchView.setQuery(currentQuery, false);
203 searchView.clearFocus();
204 }
205
206 searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
207 @Override public boolean onQueryTextSubmit(String query)
208 {
209 return true;
210 }
211
212 @Override public boolean onQueryTextChange(String s)
213 {
214 viewModel.loadBookmarks(s);
215 return true;
216 }
217 });
218 }
219 }
220}