33 private static final String TAG =
"BookmarkViewModel";
36 private final MutableLiveData<BookmarkBase> bookmarkLiveData =
new MutableLiveData<>();
37 private final MutableLiveData<Boolean> saveCompleteEvent =
new MutableLiveData<>();
39 private boolean settingsChanged =
false;
40 private boolean newBookmark =
false;
43 private final ExecutorService executor = Executors.newSingleThreadExecutor();
45 private final ManualBookmarkGateway manualBookmarkGateway;
46 private final QuickConnectHistoryGateway quickConnectHistoryGateway;
51 manualBookmarkGateway =
52 new ManualBookmarkGateway(AppDatabase.getInstance(application).bookmarkDao());
53 quickConnectHistoryGateway =
54 new QuickConnectHistoryGateway(HistoryDatabase.getInstance(application).historyDao());
57 public LiveData<BookmarkBase> getBookmarkLiveData()
59 return bookmarkLiveData;
62 public LiveData<Boolean> getSaveCompleteEvent()
64 return saveCompleteEvent;
67 public BookmarkBase getBookmark()
69 return bookmarkLiveData.getValue();
72 public boolean isSettingsChanged()
74 return settingsChanged;
77 public void setSettingsChanged(
boolean changed)
79 settingsChanged = changed;
82 public boolean isNewBookmark()
87 public void setNewBookmark(
boolean isNew)
96 public void loadBookmark(Bundle bundle, SharedPreferences prefs)
98 if (bookmarkLiveData.getValue() !=
null)
101 executor.execute(() -> {
102 BookmarkBase bookmark =
null;
103 boolean isNew =
false;
105 if (bundle !=
null && bundle.containsKey(
BookmarkActivity.PARAM_CONNECTION_REFERENCE))
107 String refStr = bundle.getString(BookmarkActivity.PARAM_CONNECTION_REFERENCE);
109 if (ConnectionReference.isBookmarkReference(refStr))
112 manualBookmarkGateway.findById(ConnectionReference.getBookmarkId(refStr));
115 else if (ConnectionReference.isHostnameReference(refStr))
117 bookmark = new BookmarkBase();
118 bookmark.setLabel(ConnectionReference.getHostname(refStr));
119 bookmark.setHostname(ConnectionReference.getHostname(refStr));
122 else if (ConnectionReference.isFileReference(refStr))
124 String file = ConnectionReference.getFile(refStr);
125 bookmark = new BookmarkBase();
126 bookmark.setLabel(file);
129 RDPFileParser rdpFile = new RDPFileParser(file);
130 updateBookmarkFromFile(bookmark, rdpFile);
131 bookmark.setLabel(new File(file).getName());
134 catch (IOException e)
136 Log.e(TAG,
"Failed reading RDP file", e);
141 if (bookmark ==
null)
143 bookmark =
new BookmarkBase();
146 this.newBookmark = isNew;
147 this.settingsChanged =
false;
150 prefs.edit().clear().apply();
151 bookmark.writeToSharedPreferences(prefs);
154 bookmarkLiveData.postValue(bookmark);
158 public void saveBookmark(SharedPreferences prefs)
160 BookmarkBase bookmark = getBookmark();
161 if (bookmark ==
null)
164 executor.execute(() -> {
165 bookmark.readFromSharedPreferences(prefs);
167 if (bookmark.getType() == BookmarkBase.TYPE_MANUAL)
169 quickConnectHistoryGateway.removeHistoryItem(bookmark.getHostname());
171 if (bookmark.getId() > 0)
173 manualBookmarkGateway.update(bookmark);
177 manualBookmarkGateway.insert(bookmark);
182 saveCompleteEvent.postValue(
true);
186 private void updateBookmarkFromFile(BookmarkBase bookmark, RDPFileParser rdpFile)
191 s = rdpFile.getString(
"full address");
194 if (s.lastIndexOf(
":") > s.lastIndexOf(
"]"))
198 bookmark.setPort(Integer.parseInt(s.substring(s.lastIndexOf(
":") + 1)));
200 catch (NumberFormatException e)
202 Log.e(TAG,
"Malformed address");
204 s = s.substring(0, s.lastIndexOf(
":"));
206 if (s.startsWith(
"[") && s.endsWith(
"]"))
207 s = s.substring(1, s.length() - 1);
208 bookmark.setHostname(s);
211 i = rdpFile.getInteger(
"server port");
215 s = rdpFile.getString(
"username");
217 bookmark.setUsername(s);
219 s = rdpFile.getString(
"domain");
221 bookmark.setDomain(s);
223 i = rdpFile.getInteger(
"connect to console");
225 bookmark.getAdvancedSettings().setConsoleMode(i == 1);
228 @Override
protected void onCleared()