ArbitraryDataProvider.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /**
  2. * Nextcloud Android client application
  3. *
  4. * Copyright (C) 2017 Tobias Kaminsky
  5. * Copyright (C) 2017 Mario Danic
  6. * Copyright (C) 2017 Nextcloud.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. * <p>
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. * <p>
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.datamodel;
  22. import android.accounts.Account;
  23. import android.content.ContentResolver;
  24. import android.content.ContentValues;
  25. import android.database.Cursor;
  26. import android.net.Uri;
  27. import com.owncloud.android.db.ProviderMeta;
  28. import com.owncloud.android.lib.common.utils.Log_OC;
  29. import java.util.List;
  30. import androidx.annotation.NonNull;
  31. /**
  32. * Database provider for handling the persistence aspects of arbitrary data table.
  33. */
  34. public class ArbitraryDataProvider {
  35. private static final String TAG = ArbitraryDataProvider.class.getSimpleName();
  36. private static final String TRUE = "true";
  37. private ContentResolver contentResolver;
  38. public ArbitraryDataProvider(ContentResolver contentResolver) {
  39. if (contentResolver == null) {
  40. throw new IllegalArgumentException("Cannot create an instance with a NULL contentResolver");
  41. }
  42. this.contentResolver = contentResolver;
  43. }
  44. public int deleteKeyForAccount(String account, String key) {
  45. return contentResolver.delete(
  46. ProviderMeta.ProviderTableMeta.CONTENT_URI_ARBITRARY_DATA,
  47. ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID + " = ? AND " +
  48. ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_KEY + "= ?",
  49. new String[]{account, key}
  50. );
  51. }
  52. public int deleteForKeyWhereAccountNotIn(List<String> accounts, String key) {
  53. return contentResolver.delete(
  54. ProviderMeta.ProviderTableMeta.CONTENT_URI_ARBITRARY_DATA,
  55. ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID + " NOT IN (?) AND " +
  56. ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_KEY + "= ?",
  57. new String[]{String.valueOf(accounts), key}
  58. );
  59. }
  60. public void storeOrUpdateKeyValue(String accountName, String key, long newValue) {
  61. storeOrUpdateKeyValue(accountName, key, String.valueOf(newValue));
  62. }
  63. public void storeOrUpdateKeyValue(String accountName, String key, String newValue) {
  64. ArbitraryDataSet data = getArbitraryDataSet(accountName, key);
  65. if (data == null) {
  66. Log_OC.v(TAG, "Adding arbitrary data with cloud id: " + accountName + " key: " + key
  67. + " value: " + newValue);
  68. ContentValues cv = new ContentValues();
  69. cv.put(ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID, accountName);
  70. cv.put(ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_KEY, key);
  71. cv.put(ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_VALUE, newValue);
  72. Uri result = contentResolver.insert(ProviderMeta.ProviderTableMeta.CONTENT_URI_ARBITRARY_DATA, cv);
  73. if (result == null) {
  74. Log_OC.v(TAG, "Failed to store arbitrary data with cloud id: " + accountName + " key: " + key
  75. + " value: " + newValue);
  76. }
  77. } else {
  78. Log_OC.v(TAG, "Updating arbitrary data with cloud id: " + accountName + " key: " + key
  79. + " value: " + newValue);
  80. ContentValues cv = new ContentValues();
  81. cv.put(ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID, data.getCloudId());
  82. cv.put(ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_KEY, data.getKey());
  83. cv.put(ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_VALUE, newValue);
  84. int result = contentResolver.update(
  85. ProviderMeta.ProviderTableMeta.CONTENT_URI_ARBITRARY_DATA,
  86. cv,
  87. ProviderMeta.ProviderTableMeta._ID + "=?",
  88. new String[]{String.valueOf(data.getId())}
  89. );
  90. if (result == 0) {
  91. Log_OC.v(TAG, "Failed to update arbitrary data with cloud id: " + accountName + " key: " + key
  92. + " value: " + newValue);
  93. }
  94. }
  95. }
  96. public Long getLongValue(String accountName, String key) {
  97. String value = getValue(accountName, key);
  98. if (value.isEmpty()) {
  99. return -1l;
  100. } else {
  101. return Long.valueOf(value);
  102. }
  103. }
  104. public Long getLongValue(Account account, String key) {
  105. return getLongValue(account.name, key);
  106. }
  107. public boolean getBooleanValue(String accountName, String key) {
  108. return TRUE.equalsIgnoreCase(getValue(accountName, key));
  109. }
  110. public boolean getBooleanValue(Account account, String key) {
  111. return getBooleanValue(account.name, key);
  112. }
  113. /**
  114. * returns integer if found else -1
  115. *
  116. * @param accountName
  117. * @param key
  118. * @return
  119. */
  120. public Integer getIntegerValue(String accountName, String key) {
  121. String value = getValue(accountName, key);
  122. if (value.isEmpty()) {
  123. return -1;
  124. } else {
  125. return Integer.valueOf(value);
  126. }
  127. }
  128. /**
  129. * returns integer if found else -1
  130. *
  131. * @param account
  132. * @param key
  133. * @return
  134. */
  135. public Integer getIntegerValue(Account account, String key) {
  136. return getIntegerValue(account.name, key);
  137. }
  138. /**
  139. * Returns stored value as string or empty string
  140. * @return string if value found or empty string
  141. */
  142. @NonNull
  143. public String getValue(Account account, String key) {
  144. return account != null ? getValue(account.name, key) : "";
  145. }
  146. public String getValue(String accountName, String key) {
  147. Cursor cursor = contentResolver.query(
  148. ProviderMeta.ProviderTableMeta.CONTENT_URI_ARBITRARY_DATA,
  149. null,
  150. ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID + " = ? and " +
  151. ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_KEY + " = ?",
  152. new String[]{accountName, key},
  153. null
  154. );
  155. if (cursor != null) {
  156. if (cursor.moveToFirst()) {
  157. String value = cursor.getString(cursor.getColumnIndex(
  158. ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_VALUE));
  159. if (value == null) {
  160. Log_OC.e(TAG, "Arbitrary value could not be created from cursor");
  161. } else {
  162. cursor.close();
  163. return value;
  164. }
  165. }
  166. cursor.close();
  167. return "";
  168. } else {
  169. Log_OC.e(TAG, "DB error restoring arbitrary values.");
  170. }
  171. return "";
  172. }
  173. private ArbitraryDataSet getArbitraryDataSet(String accountName, String key) {
  174. Cursor cursor = contentResolver.query(
  175. ProviderMeta.ProviderTableMeta.CONTENT_URI_ARBITRARY_DATA,
  176. null,
  177. ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID + " = ? and " +
  178. ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_KEY + " = ?",
  179. new String[]{accountName, key},
  180. null
  181. );
  182. ArbitraryDataSet dataSet = null;
  183. if (cursor != null) {
  184. if (cursor.moveToFirst()) {
  185. int id = cursor.getInt(cursor.getColumnIndex(ProviderMeta.ProviderTableMeta._ID));
  186. String dbAccount = cursor.getString(cursor.getColumnIndex(
  187. ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID));
  188. String dbKey = cursor.getString(cursor.getColumnIndex(
  189. ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_KEY));
  190. String dbValue = cursor.getString(cursor.getColumnIndex(
  191. ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_VALUE));
  192. if (id == -1) {
  193. Log_OC.e(TAG, "Arbitrary value could not be created from cursor");
  194. } else {
  195. dataSet = new ArbitraryDataSet(id, dbAccount, dbKey, dbValue);
  196. }
  197. }
  198. cursor.close();
  199. } else {
  200. Log_OC.e(TAG, "DB error restoring arbitrary values.");
  201. }
  202. return dataSet;
  203. }
  204. public class ArbitraryDataSet {
  205. private int id;
  206. private String cloudId;
  207. private String key;
  208. private String value;
  209. public ArbitraryDataSet(int id, String cloudId, String key, String value) {
  210. this.id = id;
  211. this.cloudId = cloudId;
  212. this.key = key;
  213. this.value = value;
  214. }
  215. public int getId() {
  216. return id;
  217. }
  218. public String getCloudId() {
  219. return cloudId;
  220. }
  221. public String getKey() {
  222. return key;
  223. }
  224. public String getValue() {
  225. return value;
  226. }
  227. }
  228. }