Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.42 KB | None | 0 0
  1. package tubemate.tubebox.torrent.database.tables;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.net.Uri;
  7. import android.support.annotation.NonNull;
  8. import android.text.TextUtils;
  9. import android.util.Log;
  10.  
  11. import java.io.File;
  12. import java.util.Locale;
  13.  
  14. import tubemate.tubebox.base.providers.media.models.Media;
  15. import tubemate.tubebox.torrent.DownloadInfo;
  16. import tubemate.tubebox.torrent.Torrent;
  17. import tubemate.tubebox.torrent.database.DBUtils;
  18.  
  19.  
  20. public final class Downloads extends Table {
  21.  
  22. public static final String NAME = "downloads";
  23.  
  24. public static final String _TYPE = "_type";
  25. public static final String _IMDB = "_imdb";
  26. public static final String _TORRENT_URL = "_torrent_url";
  27. public static final String _TORRENT_MAGNET = "_torrent_magnet";
  28. public static final String _FILE_NAME = "_file_name";
  29. public static final String _POSTER_URL = "_poster_url";
  30. public static final String _TITLE = "_title";
  31. public static final String _DIRECTORY = "_directory";
  32. public static final String _TORRENT_FILE_PATH = "_torrent_file_path";
  33. public static final String _STATE = "_state";
  34. public static final String _SIZE = "_size";
  35. public static final String _SEASON = "_season";
  36. public static final String _EPISODE = "_episode";
  37. public static final String _TORRENT_HASH = "_torrent_hash";
  38. public static final String _RESUME_DATA = "_resume_data";
  39. public static final String _READY_TO_WATCH = "_ready_to_watch";
  40. public static final String _VIDEO_FILE_PATH="_video_file_path";
  41. public static final Uri CONTENT_URI = getContentUri(NAME);
  42.  
  43. private static final String FORMAT_TORRENT_HASH_SELECTION = Downloads._TORRENT_HASH + "=\"%s\" COLLATE NOCASE";
  44. private static final String FORMAT_VIDEO_DOWNLOAD_SELECTION = FORMAT_TORRENT_HASH_SELECTION + " OR ( " + Downloads._FILE_NAME + "=\"%s\" AND " + Downloads._SIZE + "=%d )";
  45. private static final String FORMAT_TV_SHOW_DOWNLOAD_SELECTION = "( " + FORMAT_VIDEO_DOWNLOAD_SELECTION + " ) AND " + Downloads._SEASON + "=%d AND " + Downloads._EPISODE + "=%d";
  46.  
  47. public static String createTableQuery() {
  48. return "CREATE TABLE " + NAME + " ("
  49. + _ID + " INTEGER PRIMARY KEY, "
  50. + _TYPE + " TEXT, "
  51. + _IMDB + " TEXT, "
  52. + _TORRENT_URL + " TEXT, "
  53. + _TORRENT_MAGNET + " TEXT, "
  54. + _FILE_NAME + " TEXT, "
  55. + _POSTER_URL + " TEXT, "
  56. + _TITLE + " TEXT, "
  57. + _DIRECTORY + " TEXT, "
  58. + _TORRENT_FILE_PATH + " TEXT, "
  59. + _STATE + " INTEGER, "
  60. + _SIZE + " INTEGER, "
  61. + _SEASON + " INTEGER, "
  62. + _EPISODE + " INTEGER, "
  63. + _TORRENT_HASH + " TEXT, "
  64. + _RESUME_DATA + " BLOB, "
  65. + _READY_TO_WATCH + " INTEGER, "
  66. +_VIDEO_FILE_PATH+" TEXT)";
  67. }
  68.  
  69. public static Cursor query(Context context, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
  70. return context.getContentResolver().query(CONTENT_URI, projection, selection, selectionArgs, sortOrder);
  71. }
  72.  
  73. public static Uri insert(Context context, DownloadInfo info) {
  74. return context.getContentResolver().insert(CONTENT_URI, buildValues(info));
  75. }
  76.  
  77. public static int update(Context context, DownloadInfo info) {
  78. Log.w("update",info.videoFileName);
  79. int i= context.getContentResolver().update(getContentUri(NAME, info.id), buildValues(info), null, null);
  80.  
  81. return i;
  82. }
  83.  
  84. public static int update(Context context, String hash, byte[] resumeData) {
  85. final ContentValues values = new ContentValues();
  86. values.put(_RESUME_DATA, resumeData);
  87. return context.getContentResolver().update(CONTENT_URI, values, String.format(Locale.ENGLISH, FORMAT_TORRENT_HASH_SELECTION, hash), null);
  88. }
  89.  
  90. public static int readyToWatch(Context context, long id) {
  91. final ContentValues values = new ContentValues();
  92. values.put(_READY_TO_WATCH, true);
  93. return context.getContentResolver().update(getContentUri(NAME, id), values, null, null);
  94. }
  95. public static int delete2(Context context, DownloadInfo info) {
  96. return context.getContentResolver().delete(getContentUri(NAME, info.id), null, null);
  97. }
  98. public static int delete(Context context, DownloadInfo info) {
  99. Log.w("fileobserver","db"+info.id);
  100. return context.getContentResolver().delete(getContentUri(NAME, info.id), null, null);
  101. }
  102.  
  103. public static boolean isDownloads(@NonNull Context context, @NonNull Torrent torrent, int season, int episode) {
  104. final String selection;
  105. if (!TextUtils.isEmpty(torrent.getHash()) && !TextUtils.isEmpty(torrent.getFile()) && torrent.getSize() > 0) {
  106. if (season >= 0 && episode >= 0) {
  107. selection = String.format(Locale.ENGLISH, FORMAT_TV_SHOW_DOWNLOAD_SELECTION, torrent.getHash(), torrent.getFile(), torrent.getSize(), season, episode);
  108. } else {
  109. selection = String.format(Locale.ENGLISH, FORMAT_VIDEO_DOWNLOAD_SELECTION, torrent.getHash(), torrent.getFile(), torrent.getSize());
  110. }
  111. } else {
  112. selection = Downloads._TORRENT_URL + "=\"" + torrent.getUrl() + "\" OR " + Downloads._TORRENT_MAGNET + "=\"" + torrent.getMagnet() + "\"";
  113. }
  114. final Cursor cursor = Downloads.query(context, null, selection, null, null);
  115. if (cursor != null) {
  116. if (cursor.getCount() > 0) {
  117. cursor.close();
  118. return true;
  119. }
  120. cursor.close();
  121. }
  122. return false;
  123. }
  124.  
  125. // public static boolean isInDownloads(@NonNull Context context, @NonNull String path) {
  126. // final String selection;
  127. //
  128. // selection = Downloads._TORRENT_FILE_PATH + "=\"" + path + "\" OR " + Downloads._TORRENT_MAGNET + "=\"" + torrent.getMagnet() + "\"";
  129. //
  130. // final Cursor cursor = Downloads.query(context, null, selection, null, null);
  131. // if (cursor != null) {
  132. // if (cursor.getCount() > 0) {
  133. // cursor.close();
  134. // return true;
  135. // }
  136. // cursor.close();
  137. // }
  138. // return false;
  139. // }
  140. public static DownloadInfo getDownload(Cursor cursor){
  141. DownloadInfo info=new DownloadInfo();
  142. try {
  143. info.id = cursor.getLong(cursor.getColumnIndexOrThrow(_ID));
  144. info.videoFileName=cursor.getString(cursor.getColumnIndex(_VIDEO_FILE_PATH));
  145. info.type = cursor.getString(cursor.getColumnIndexOrThrow(_TYPE));
  146. info.imdb = cursor.getString(cursor.getColumnIndexOrThrow(_IMDB));
  147. info.torrentUrl = cursor.getString(cursor.getColumnIndexOrThrow(_TORRENT_URL));
  148. info.torrentMagnet = cursor.getString(cursor.getColumnIndexOrThrow(_TORRENT_MAGNET));
  149. info.fileName = cursor.getString(cursor.getColumnIndexOrThrow(_FILE_NAME));
  150. info.posterUrl = cursor.getString(cursor.getColumnIndexOrThrow(_POSTER_URL));
  151. info.title = cursor.getString(cursor.getColumnIndexOrThrow(_TITLE));
  152. info.directory = new File(cursor.getString(cursor.getColumnIndexOrThrow(_DIRECTORY)));
  153. info.torrentFilePath = cursor.getString(cursor.getColumnIndexOrThrow(_TORRENT_FILE_PATH));
  154. info.state = cursor.getInt(cursor.getColumnIndexOrThrow(_STATE));
  155. info.size = cursor.getLong(cursor.getColumnIndexOrThrow(_SIZE));
  156. info.season = cursor.getInt(cursor.getColumnIndexOrThrow(_SEASON));
  157. info.episode = cursor.getInt(cursor.getColumnIndexOrThrow(_EPISODE));
  158. info.torrentHash = cursor.getString(cursor.getColumnIndexOrThrow(_TORRENT_HASH));
  159. info.resumeData = cursor.getBlob(cursor.getColumnIndexOrThrow(_RESUME_DATA));
  160. info.readyToWatch = DBUtils.getInt(cursor, _READY_TO_WATCH, 0) != 0;
  161. } catch (Exception e) {
  162. e.printStackTrace();
  163. }
  164. return info;
  165. }
  166. public static void populate(DownloadInfo info, Cursor cursor) {
  167. try {
  168. info.id = cursor.getLong(cursor.getColumnIndexOrThrow(_ID));
  169. info.videoFileName=cursor.getString(cursor.getColumnIndex(_VIDEO_FILE_PATH));
  170. info.type = cursor.getString(cursor.getColumnIndexOrThrow(_TYPE));
  171. info.imdb = cursor.getString(cursor.getColumnIndexOrThrow(_IMDB));
  172. info.torrentUrl = cursor.getString(cursor.getColumnIndexOrThrow(_TORRENT_URL));
  173. info.torrentMagnet = cursor.getString(cursor.getColumnIndexOrThrow(_TORRENT_MAGNET));
  174. info.fileName = cursor.getString(cursor.getColumnIndexOrThrow(_FILE_NAME));
  175. info.posterUrl = cursor.getString(cursor.getColumnIndexOrThrow(_POSTER_URL));
  176. info.title = cursor.getString(cursor.getColumnIndexOrThrow(_TITLE));
  177. info.directory = new File(cursor.getString(cursor.getColumnIndexOrThrow(_DIRECTORY)));
  178. info.torrentFilePath = cursor.getString(cursor.getColumnIndexOrThrow(_TORRENT_FILE_PATH));
  179. info.state = cursor.getInt(cursor.getColumnIndexOrThrow(_STATE));
  180. info.size = cursor.getLong(cursor.getColumnIndexOrThrow(_SIZE));
  181. info.season = cursor.getInt(cursor.getColumnIndexOrThrow(_SEASON));
  182. info.episode = cursor.getInt(cursor.getColumnIndexOrThrow(_EPISODE));
  183. info.torrentHash = cursor.getString(cursor.getColumnIndexOrThrow(_TORRENT_HASH));
  184. info.resumeData = cursor.getBlob(cursor.getColumnIndexOrThrow(_RESUME_DATA));
  185. info.readyToWatch = DBUtils.getInt(cursor, _READY_TO_WATCH, 0) != 0;
  186. } catch (Exception e) {
  187. e.printStackTrace();
  188. }
  189.  
  190.  
  191. }
  192. public static DownloadInfo getInfo(Cursor cursor){
  193. DownloadInfo info=new DownloadInfo();
  194. try {
  195. info.id = cursor.getLong(cursor.getColumnIndexOrThrow(_ID));
  196. info.type = cursor.getString(cursor.getColumnIndexOrThrow(_TYPE));
  197. info.imdb = cursor.getString(cursor.getColumnIndexOrThrow(_IMDB));
  198. info.torrentUrl = cursor.getString(cursor.getColumnIndexOrThrow(_TORRENT_URL));
  199. info.torrentMagnet = cursor.getString(cursor.getColumnIndexOrThrow(_TORRENT_MAGNET));
  200. info.fileName = cursor.getString(cursor.getColumnIndexOrThrow(_FILE_NAME));
  201. info.posterUrl = cursor.getString(cursor.getColumnIndexOrThrow(_POSTER_URL));
  202. info.title = cursor.getString(cursor.getColumnIndexOrThrow(_TITLE));
  203. info.directory = new File(cursor.getString(cursor.getColumnIndexOrThrow(_DIRECTORY)));
  204. info.torrentFilePath = cursor.getString(cursor.getColumnIndexOrThrow(_TORRENT_FILE_PATH));
  205. info.state = cursor.getInt(cursor.getColumnIndexOrThrow(_STATE));
  206. info.size = cursor.getLong(cursor.getColumnIndexOrThrow(_SIZE));
  207. info.season = cursor.getInt(cursor.getColumnIndexOrThrow(_SEASON));
  208. info.episode = cursor.getInt(cursor.getColumnIndexOrThrow(_EPISODE));
  209. info.torrentHash = cursor.getString(cursor.getColumnIndexOrThrow(_TORRENT_HASH));
  210. info.resumeData = cursor.getBlob(cursor.getColumnIndexOrThrow(_RESUME_DATA));
  211. info.readyToWatch = DBUtils.getInt(cursor, _READY_TO_WATCH, 0) != 0;
  212. } catch (Exception e) {
  213. e.printStackTrace();
  214. }
  215. return info;
  216. }
  217. private static ContentValues buildValues(DownloadInfo info) {
  218. ContentValues values = new ContentValues();
  219. values.put(_TYPE, info.type);
  220. values.put(_IMDB, info.imdb);
  221. values.put(_TORRENT_URL, info.torrentUrl);
  222. values.put(_TORRENT_MAGNET, info.torrentMagnet);
  223. values.put(_FILE_NAME, info.fileName);
  224. values.put(_POSTER_URL, info.posterUrl);
  225. values.put(_TITLE, info.title);
  226. values.put(_DIRECTORY, info.directory.getAbsolutePath());
  227. Log.w("file1",info.directory.getAbsolutePath());
  228.  
  229.  
  230. values.put(_TORRENT_FILE_PATH, info.torrentFilePath);
  231. values.put(_STATE, info.state);
  232. values.put(_SIZE, info.size);
  233. values.put(_SEASON, info.season);
  234. values.put(_EPISODE, info.episode);
  235. values.put(_TORRENT_HASH, info.torrentHash);
  236. values.put(_RESUME_DATA, info.resumeData);
  237. return values;
  238. }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement