ArbitraryDataProvider.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. *
  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. *
  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.content.ContentResolver;
  23. import android.content.ContentValues;
  24. import android.database.Cursor;
  25. import android.net.Uri;
  26. import com.nextcloud.client.account.User;
  27. import com.owncloud.android.db.ProviderMeta;
  28. import com.owncloud.android.lib.common.utils.Log_OC;
  29. import androidx.annotation.NonNull;
  30. import androidx.annotation.Nullable;
  31. /**
  32. * Database provider for handling the persistence aspects of arbitrary data table.
  33. */
  34. public class ArbitraryDataProvider {
  35. public static final String DIRECT_EDITING = "DIRECT_EDITING";
  36. public static final String DIRECT_EDITING_ETAG = "DIRECT_EDITING_ETAG";
  37. public static final String PREDEFINED_STATUS = "PREDEFINED_STATUS";
  38. private static final String TAG = ArbitraryDataProvider.class.getSimpleName();
  39. private static final String TRUE = "true";
  40. private ContentResolver contentResolver;
  41. public ArbitraryDataProvider(ContentResolver contentResolver) {
  42. if (contentResolver == null) {
  43. throw new IllegalArgumentException("Cannot create an instance with a NULL contentResolver");
  44. }
  45. this.contentResolver = contentResolver;
  46. }
  47. public int deleteKeyForAccount(String account, String key) {
  48. return contentResolver.delete(
  49. ProviderMeta.ProviderTableMeta.CONTENT_URI_ARBITRARY_DATA,
  50. ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID + " = ? AND " +
  51. ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_KEY + "= ?",
  52. new String[]{account, key}
  53. );
  54. }
  55. public void storeOrUpdateKeyValue(String accountName, String key, long newValue) {
  56. storeOrUpdateKeyValue(accountName, key, String.valueOf(newValue));
  57. }
  58. public void storeOrUpdateKeyValue(@NonNull String accountName,
  59. @NonNull String key,
  60. @Nullable String newValue) {
  61. ArbitraryDataSet data = getArbitraryDataSet(accountName, key);
  62. String value;
  63. if (newValue == null) {
  64. value = "";
  65. } else {
  66. value = newValue;
  67. }
  68. if (data == null) {
  69. Log_OC.v(TAG, "Adding arbitrary data with cloud id: " + accountName + " key: " + key
  70. + " value: " + value);
  71. ContentValues cv = new ContentValues();
  72. cv.put(ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID, accountName);
  73. cv.put(ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_KEY, key);
  74. cv.put(ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_VALUE, value);
  75. Uri result = contentResolver.insert(ProviderMeta.ProviderTableMeta.CONTENT_URI_ARBITRARY_DATA, cv);
  76. if (result == null) {
  77. Log_OC.v(TAG, "Failed to store arbitrary data with cloud id: " + accountName + " key: " + key
  78. + " value: " + value);
  79. }
  80. } else {
  81. Log_OC.v(TAG, "Updating arbitrary data with cloud id: " + accountName + " key: " + key
  82. + " value: " + value);
  83. ContentValues cv = new ContentValues();
  84. cv.put(ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID, data.getCloudId());
  85. cv.put(ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_KEY, data.getKey());
  86. cv.put(ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_VALUE, value);
  87. int result = contentResolver.update(
  88. ProviderMeta.ProviderTableMeta.CONTENT_URI_ARBITRARY_DATA,
  89. cv,
  90. ProviderMeta.ProviderTableMeta._ID + "=?",
  91. new String[]{String.valueOf(data.getId())}
  92. );
  93. if (result == 0) {
  94. Log_OC.v(TAG, "Failed to update arbitrary data with cloud id: " + accountName + " key: " + key
  95. + " value: " + value);
  96. }
  97. }
  98. }
  99. Long getLongValue(String accountName, String key) {
  100. String value = getValue(accountName, key);
  101. if (value.isEmpty()) {
  102. return -1L;
  103. } else {
  104. return Long.valueOf(value);
  105. }
  106. }
  107. public Long getLongValue(User user, String key) {
  108. return getLongValue(user.getAccountName(), key);
  109. }
  110. public boolean getBooleanValue(String accountName, String key) {
  111. return TRUE.equalsIgnoreCase(getValue(accountName, key));
  112. }
  113. public boolean getBooleanValue(User user, String key) {
  114. return getBooleanValue(user.getAccountName(), key);
  115. }
  116. /**
  117. * returns integer if found else -1
  118. *
  119. * @param accountName name of account
  120. * @param key key to get value for
  121. * @return Integer specified by account and key
  122. */
  123. public Integer getIntegerValue(String accountName, String key) {
  124. String value = getValue(accountName, key);
  125. if (value.isEmpty()) {
  126. return -1;
  127. } else {
  128. return Integer.valueOf(value);
  129. }
  130. }
  131. /**
  132. * Returns stored value as string or empty string
  133. *
  134. * @return string if value found or empty string
  135. */
  136. @NonNull
  137. public String getValue(@Nullable User user, String key) {
  138. return user != null ? getValue(user.getAccountName(), key) : "";
  139. }
  140. public String getValue(String accountName, String key) {
  141. Cursor cursor = contentResolver.query(
  142. ProviderMeta.ProviderTableMeta.CONTENT_URI_ARBITRARY_DATA,
  143. null,
  144. ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID + " = ? and " +
  145. ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_KEY + " = ?",
  146. new String[]{accountName, key},
  147. null
  148. );
  149. if (cursor != null) {
  150. if (cursor.moveToFirst()) {
  151. String value = cursor.getString(cursor.getColumnIndex(
  152. ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_VALUE));
  153. if (value == null) {
  154. Log_OC.e(TAG, "Arbitrary value could not be created from cursor");
  155. } else {
  156. cursor.close();
  157. return value;
  158. }
  159. }
  160. cursor.close();
  161. return "";
  162. } else {
  163. Log_OC.e(TAG, "DB error restoring arbitrary values.");
  164. }
  165. return "";
  166. }
  167. private ArbitraryDataSet getArbitraryDataSet(String accountName, String key) {
  168. Cursor cursor = contentResolver.query(
  169. ProviderMeta.ProviderTableMeta.CONTENT_URI_ARBITRARY_DATA,
  170. null,
  171. ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID + " = ? and " +
  172. ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_KEY + " = ?",
  173. new String[]{accountName, key},
  174. null
  175. );
  176. ArbitraryDataSet dataSet = null;
  177. if (cursor != null) {
  178. if (cursor.moveToFirst()) {
  179. int id = cursor.getInt(cursor.getColumnIndex(ProviderMeta.ProviderTableMeta._ID));
  180. String dbAccount = cursor.getString(cursor.getColumnIndex(
  181. ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID));
  182. String dbKey = cursor.getString(cursor.getColumnIndex(
  183. ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_KEY));
  184. String dbValue = cursor.getString(cursor.getColumnIndex(
  185. ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_VALUE));
  186. if (id == -1) {
  187. Log_OC.e(TAG, "Arbitrary value could not be created from cursor");
  188. } else {
  189. if (dbValue == null) {
  190. dbValue = "";
  191. }
  192. dataSet = new ArbitraryDataSet(id, dbAccount, dbKey, dbValue);
  193. }
  194. }
  195. cursor.close();
  196. } else {
  197. Log_OC.e(TAG, "DB error restoring arbitrary values.");
  198. }
  199. return dataSet;
  200. }
  201. }