40 private static final String TAG =
"BookmarkViewModel";
43 private final MutableLiveData<BookmarkBase> bookmarkLiveData =
new MutableLiveData<>();
44 private final MutableLiveData<Boolean> saveCompleteEvent =
new MutableLiveData<>();
46 private boolean settingsChanged =
false;
47 private boolean newBookmark =
false;
50 private final ExecutorService executor = Executors.newSingleThreadExecutor();
52 private final ManualBookmarkGateway manualBookmarkGateway;
53 private final QuickConnectHistoryGateway quickConnectHistoryGateway;
58 manualBookmarkGateway =
59 new ManualBookmarkGateway(AppDatabase.getInstance(application).bookmarkDao());
60 quickConnectHistoryGateway =
61 new QuickConnectHistoryGateway(HistoryDatabase.getInstance(application).historyDao());
64 public LiveData<BookmarkBase> getBookmarkLiveData()
66 return bookmarkLiveData;
69 public LiveData<Boolean> getSaveCompleteEvent()
71 return saveCompleteEvent;
74 public BookmarkBase getBookmark()
76 return bookmarkLiveData.getValue();
79 public boolean isSettingsChanged()
81 return settingsChanged;
84 public void setSettingsChanged(
boolean changed)
86 settingsChanged = changed;
89 public boolean isNewBookmark()
94 public void setNewBookmark(
boolean isNew)
103 public void loadBookmark(Bundle bundle, SharedPreferences prefs)
105 if (bookmarkLiveData.getValue() !=
null)
108 executor.execute(() -> {
109 BookmarkBase bookmark =
null;
110 boolean isNew =
false;
112 if (bundle !=
null && bundle.containsKey(
BookmarkActivity.PARAM_CONNECTION_REFERENCE))
114 String refStr = bundle.getString(BookmarkActivity.PARAM_CONNECTION_REFERENCE);
116 if (ConnectionReference.isBookmarkReference(refStr))
119 manualBookmarkGateway.findById(ConnectionReference.getBookmarkId(refStr));
122 else if (ConnectionReference.isHostnameReference(refStr))
124 bookmark = new BookmarkBase();
125 bookmark.setLabel(ConnectionReference.getHostname(refStr));
126 bookmark.setHostname(ConnectionReference.getHostname(refStr));
129 else if (ConnectionReference.isFileReference(refStr))
131 String fileRef = ConnectionReference.getFile(refStr);
132 bookmark = new BookmarkBase();
133 String name = Uri.decode(new File(fileRef).getName());
134 String lower = name.toLowerCase(java.util.Locale.ROOT);
135 if (lower.endsWith(
".rdp"))
136 name = name.substring(0, name.length() - 4);
137 else if (lower.endsWith(
".rdpw"))
138 name = name.substring(0, name.length() - 5);
139 bookmark.setLabel(name);
142 Uri uri = Uri.parse(fileRef);
143 RDPFileHelper.importFrom(
144 getApplication().getContentResolver().openInputStream(uri), bookmark);
146 catch (IOException e)
148 Log.e(TAG,
"Failed reading RDP file", e);
154 if (bookmark ==
null)
156 bookmark =
new BookmarkBase();
159 this.newBookmark = isNew;
160 this.settingsChanged =
false;
163 prefs.edit().clear().apply();
164 bookmark.writeToSharedPreferences(prefs);
167 bookmarkLiveData.postValue(bookmark);
171 public void saveBookmark(SharedPreferences prefs)
173 BookmarkBase bookmark = getBookmark();
174 if (bookmark ==
null)
177 executor.execute(() -> {
178 bookmark.readFromSharedPreferences(prefs);
180 if (bookmark.getType() == BookmarkBase.TYPE_MANUAL)
182 quickConnectHistoryGateway.removeHistoryItem(bookmark.getHostname());
184 if (bookmark.getId() > 0)
186 manualBookmarkGateway.update(bookmark);
190 manualBookmarkGateway.insert(bookmark);
195 saveCompleteEvent.postValue(
true);
199 @Override
protected void onCleared()