PageRenderTime 145ms CodeModel.GetById 37ms RepoModel.GetById 5ms app.codeStats 0ms

/JUSTPLAYER/src/jp/co/kayo/android/localplayer/provider/ContentsUtils.java

https://bitbucket.org/kshark27/just-player
Java | 981 lines | 889 code | 60 blank | 32 comment | 219 complexity | a26428161324f3d99396ba1008de2740 MD5 | raw file
  1. package jp.co.kayo.android.localplayer.provider;
  2. /***
  3. * Copyright (c) 2010-2012 yokmama. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it under
  6. * the terms of the GNU General Public License as published by the Free Software
  7. * Foundation; either version 2 of the License, or (at your option) any later
  8. * version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  13. * details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  17. * Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. import java.io.File;
  21. import java.util.ArrayList;
  22. import java.util.Hashtable;
  23. import java.util.List;
  24. import jp.co.kayo.android.localplayer.BaseActivity;
  25. import jp.co.kayo.android.localplayer.R;
  26. import jp.co.kayo.android.localplayer.appwidget.AppWidgetHelper;
  27. import jp.co.kayo.android.localplayer.consts.MediaConsts;
  28. import jp.co.kayo.android.localplayer.consts.MediaConsts.AudioAlbum;
  29. import jp.co.kayo.android.localplayer.consts.MediaConsts.AudioArtist;
  30. import jp.co.kayo.android.localplayer.consts.MediaConsts.AudioMedia;
  31. import jp.co.kayo.android.localplayer.consts.MediaConsts.FileMedia;
  32. import jp.co.kayo.android.localplayer.consts.SystemConsts;
  33. import jp.co.kayo.android.localplayer.service.IMediaPlayerService;
  34. import jp.co.kayo.android.localplayer.service.StreamCacherServer;
  35. import jp.co.kayo.android.localplayer.util.Funcs;
  36. import jp.co.kayo.android.localplayer.util.Logger;
  37. import jp.co.kayo.android.localplayer.util.MyPreferenceManager;
  38. import jp.co.kayo.android.localplayer.util.ViewCache;
  39. import android.app.AlertDialog;
  40. import android.content.ContentUris;
  41. import android.content.Context;
  42. import android.content.DialogInterface;
  43. import android.content.Intent;
  44. import android.content.SharedPreferences;
  45. import android.content.SharedPreferences.Editor;
  46. import android.content.pm.PackageManager;
  47. import android.content.pm.ProviderInfo;
  48. import android.database.Cursor;
  49. import android.net.Uri;
  50. import android.os.Build;
  51. import android.os.Handler;
  52. import android.os.RemoteException;
  53. import android.preference.PreferenceManager;
  54. public class ContentsUtils {
  55. public static class AlbumInfo {
  56. public String album;
  57. public String album_art;
  58. public String album_key;
  59. }
  60. private static final String DS = "jp.co.kayo.android.localplayer.ds.";
  61. public static List<ProviderInfo> getDataSouce(Context context) {
  62. ArrayList<ProviderInfo> ret = new ArrayList<ProviderInfo>();
  63. PackageManager pm = context.getPackageManager();
  64. List<ProviderInfo> list = pm.queryContentProviders(null, 0, 0);
  65. for (ProviderInfo inf : list) {
  66. if (inf.authority.startsWith(DS)) {
  67. ret.add(inf);
  68. }
  69. }
  70. return ret;
  71. }
  72. public static void reloadAlbumArt(BaseActivity activity, String album_key){
  73. if (album_key != null) {
  74. Hashtable<String, String> tbl = ContentsUtils
  75. .getAlbum(activity, new String[] {
  76. AudioAlbum.ALBUM, AudioAlbum.ALBUM_KEY,
  77. AudioAlbum.ARTIST
  78. }, album_key);
  79. if (tbl != null) {
  80. ViewCache viewcache = (ViewCache) activity.getSupportFragmentManager()
  81. .findFragmentByTag(SystemConsts.TAG_CACHE);
  82. if (viewcache != null) {
  83. viewcache.releaseImage(
  84. tbl.get(AudioAlbum.ALBUM),
  85. tbl.get(AudioAlbum.ARTIST));
  86. }
  87. }
  88. }
  89. }
  90. public static void selectSource(final Context context, final SharedPreferences pref, final Handler handler) {
  91. final String contenturi = pref.getString(SystemConsts.PREF_CONTENTURI,
  92. DeviceContentProvider.MEDIA_AUTHORITY);
  93. final List<ProviderInfo> dslist = ContentsUtils.getDataSouce(context);
  94. PackageManager pm = context.getPackageManager();
  95. final ArrayList<String> items = new ArrayList<String>();
  96. if (contenturi.equals(DeviceContentProvider.MEDIA_AUTHORITY)) {
  97. items.add(context.getString(R.string.lb_sdcard) + " *");
  98. } else {
  99. items.add(context.getString(R.string.lb_sdcard));
  100. }
  101. for (ProviderInfo inf : dslist) {
  102. if (inf.authority.equals(contenturi)) {
  103. items.add(inf.loadLabel(pm) + " *");
  104. } else {
  105. items.add(inf.loadLabel(pm).toString());
  106. }
  107. }
  108. items.add(context.getString(R.string.lb_conf_add_ds_title));
  109. AlertDialog.Builder builder = new AlertDialog.Builder(context);
  110. builder.setTitle(context.getString(R.string.lb_srcdialog));
  111. builder.setItems(items.toArray(new String[items.size()]),
  112. new DialogInterface.OnClickListener() {
  113. public void onClick(DialogInterface dialog, int item) {
  114. String selecturl = null;
  115. String item_name = items.get(item);
  116. if (item_name.indexOf(context.getString(R.string.lb_sdcard)) != -1) {
  117. // SDCard
  118. selecturl = DeviceContentProvider.MEDIA_AUTHORITY;
  119. } else if (item_name
  120. .equals(context.getString(R.string.lb_conf_add_ds_title))) {
  121. Uri uri = Uri
  122. .parse("market://search?q=jp.co.kayo.android.localplayer.ds");
  123. Intent it = new Intent(Intent.ACTION_VIEW, uri);
  124. context.startActivity(it);
  125. } else {
  126. // Ampcaheまたはその他
  127. selecturl = dslist.get(item - 1).authority;
  128. boolean checkSetting = false;
  129. if (selecturl.equals(contenturi)) {
  130. // 同じURLなら設定画面を呼び出す
  131. checkSetting = true;
  132. } else {
  133. // PINGを飛ばしてだめなら設定画面を呼び出す
  134. Cursor cursor = null;
  135. try {
  136. Uri uri = Uri.parse("content://"
  137. + selecturl + "/config/ping");
  138. cursor = context.getContentResolver().query(uri,
  139. null, null, null, null);
  140. if (cursor == null) {
  141. checkSetting = true;
  142. }
  143. } finally {
  144. if (cursor != null) {
  145. cursor.close();
  146. }
  147. }
  148. }
  149. if (checkSetting) {
  150. Intent it = new Intent();
  151. it.setClassName(
  152. dslist.get(item - 1).packageName,
  153. dslist.get(item - 1).packageName
  154. + ".MainActivity");
  155. context.startActivity(it);
  156. }
  157. }
  158. if (selecturl != null && !selecturl.equals(contenturi)) {
  159. Editor editor = pref.edit();
  160. editor.putString(SystemConsts.PREF_CONTENTURI,
  161. selecturl);
  162. editor.commit();
  163. handler.sendEmptyMessage(SystemConsts.EVT_DSCHANFED);
  164. }
  165. }
  166. });
  167. AlertDialog dlg = builder.create();
  168. dlg.show();
  169. }
  170. public static AlbumInfo getAlbumArt(Context context, String artist,
  171. Hashtable<String, AlbumInfo> tbl1, Hashtable<String, AlbumInfo> tbl2) {
  172. if (artist != null) {
  173. AlbumInfo inf = tbl1.get(artist);
  174. if (inf == null) {
  175. Cursor cur = null;
  176. Cursor cur2 = null;
  177. try {
  178. inf = new AlbumInfo();
  179. inf.album = "";
  180. cur = context.getContentResolver().query(
  181. MediaConsts.ALBUM_CONTENT_URI,
  182. new String[] { AudioAlbum.ALBUM,
  183. AudioAlbum.ALBUM_ART },
  184. AudioAlbum.ARTIST + " = ?",
  185. new String[] { artist }, null);
  186. if (cur != null && cur.moveToFirst()) {
  187. do {
  188. String album = cur
  189. .getString(cur
  190. .getColumnIndex(MediaConsts.AudioAlbum.ALBUM));
  191. String album_art = cur
  192. .getString(cur
  193. .getColumnIndex(MediaConsts.AudioAlbum.ALBUM_ART));
  194. if (album_art != null
  195. && album_art.trim().length() > 0) {
  196. inf.album = album;
  197. inf.album_art = album_art;
  198. break;
  199. }
  200. } while (cur.moveToNext());
  201. } else if (!isNoCacheAction(context)) {
  202. cur2 = context.getContentResolver().query(
  203. MediaConsts.MEDIA_CONTENT_URI,
  204. new String[] { AudioMedia.ALBUM_KEY },
  205. AudioMedia.ARTIST + " = ?",
  206. new String[] { artist }, null);
  207. if (cur2 != null && cur2.moveToFirst()) {
  208. do {
  209. String album_key = cur2
  210. .getString(cur2
  211. .getColumnIndex(MediaConsts.AudioMedia.ALBUM_KEY));
  212. if (album_key != null
  213. && album_key.trim().length() > 0) {
  214. inf = getAlbumArtByAlbum(context,
  215. album_key, tbl2);
  216. break;
  217. }
  218. } while (cur2.moveToNext());
  219. }
  220. }
  221. } finally {
  222. if (cur != null) {
  223. cur.close();
  224. }
  225. if (cur2 != null) {
  226. cur2.close();
  227. }
  228. }
  229. if (inf != null) {
  230. tbl1.put(artist, inf);
  231. }
  232. }
  233. return inf;
  234. } else {
  235. return null;
  236. }
  237. }
  238. public static AlbumInfo getAlbumArtByAlbum(Context context,
  239. String album_key, Hashtable<String, AlbumInfo> tbl) {
  240. AlbumInfo inf = tbl.get(album_key);
  241. if (inf == null) {
  242. Cursor cur = null;
  243. try {
  244. cur = context.getContentResolver()
  245. .query(MediaConsts.ALBUM_CONTENT_URI,
  246. new String[] { AudioAlbum.ALBUM,
  247. AudioAlbum.ALBUM_ART },
  248. AudioAlbum.ALBUM_KEY + " = ?",
  249. new String[] { album_key }, null);
  250. if (cur != null && cur.moveToFirst()) {
  251. String album = cur.getString(cur
  252. .getColumnIndex(MediaConsts.AudioAlbum.ALBUM));
  253. String album_art = cur.getString(cur
  254. .getColumnIndex(MediaConsts.AudioAlbum.ALBUM_ART));
  255. if (album_art != null && album_art.trim().length() > 0) {
  256. inf = new AlbumInfo();
  257. inf.album = album;
  258. inf.album_art = album_art;
  259. }
  260. }
  261. } finally {
  262. if (cur != null) {
  263. cur.close();
  264. }
  265. if (inf != null) {
  266. tbl.put(album_key, inf);
  267. }
  268. }
  269. }
  270. return inf;
  271. }
  272. public static void addOrderMediaList(IMediaPlayerService binder, long id, String data) {
  273. try {
  274. binder.addMedia(id, data);
  275. binder.commit();
  276. } catch (RemoteException e) {
  277. }
  278. }
  279. public static Cursor getFileItemCursor(Context context, String data, String type){
  280. return context.getContentResolver()
  281. .query(ContentUris
  282. .withAppendedId(
  283. MediaConsts.FOLDER_CONTENT_URI,
  284. 0),
  285. null,
  286. MediaConsts.FileMedia.DATA
  287. + " = ? AND "
  288. + FileMedia.TYPE
  289. + " = ?",
  290. new String[] { data,
  291. type }, null);
  292. }
  293. public static long addOrderFolder(IMediaPlayerService binder,
  294. Context context, String path, String sortOrder) {
  295. long ret = -1;
  296. Cursor cursor = null;
  297. try {
  298. cursor = context.getContentResolver().query(
  299. MediaConsts.FOLDER_CONTENT_URI, null,
  300. FileMedia.DATA + " = ?",
  301. new String[] { path }, sortOrder);
  302. if (cursor != null && cursor.moveToFirst()) {
  303. do {
  304. String data = cursor.getString(cursor
  305. .getColumnIndex(FileMedia.DATA));
  306. String type = cursor.getString(cursor
  307. .getColumnIndex(FileMedia.TYPE));
  308. Cursor file_cur = getFileItemCursor(context, data, type);
  309. if(file_cur!=null){
  310. if(file_cur.moveToFirst()){
  311. long media_id = file_cur
  312. .getLong(file_cur
  313. .getColumnIndex(AudioMedia._ID));
  314. String media_data = file_cur
  315. .getString(file_cur
  316. .getColumnIndex(AudioMedia.DATA));
  317. if (media_id > 0) {
  318. binder.addMedia(media_id, media_data);
  319. } else {
  320. String title = file_cur
  321. .getString(file_cur
  322. .getColumnIndex(AudioMedia.TITLE));
  323. String album = file_cur
  324. .getString(file_cur
  325. .getColumnIndex(AudioMedia.ALBUM));
  326. String artist = file_cur
  327. .getString(file_cur
  328. .getColumnIndex(AudioMedia.ARTIST));
  329. if (title == null) {
  330. title = context.getString(R.string.txt_unknown_title);
  331. }
  332. if (album == null) {
  333. album = context.getString(R.string.txt_unknown_album);
  334. }
  335. if (artist == null) {
  336. artist = context.getString(R.string.txt_unknown_artist);
  337. }
  338. binder.addMediaD(media_id, 0,
  339. title, album, artist,
  340. data);
  341. }
  342. }
  343. file_cur.close();
  344. }
  345. } while (cursor.moveToNext());
  346. binder.commit();
  347. }
  348. } catch (RemoteException e) {
  349. } finally {
  350. if (cursor != null) {
  351. cursor.close();
  352. }
  353. }
  354. return ret;
  355. }
  356. public static boolean playMediaReplace(String contentkey,
  357. IMediaPlayerService binder, Cursor cursor, String cname1,
  358. String cname2, String cname3, int position) {
  359. try {
  360. if (!binder.setContentsKey(contentkey)) {
  361. // 既に設定中のリストだった
  362. int pos = binder.getPosition();
  363. if (pos != position) {// 同じ位置をクリックした
  364. binder.lockUpdateToPlay();
  365. try {
  366. binder.reset();
  367. binder.setPosition(position);
  368. } finally {
  369. binder.play();
  370. }
  371. return true;
  372. }
  373. } else {
  374. binder.lockUpdateToPlay();
  375. try {
  376. binder.clear();
  377. cursor.moveToFirst();
  378. do {
  379. long id = cursor.getLong(cursor.getColumnIndex(cname1));
  380. String media_data = cursor
  381. .getString(cursor
  382. .getColumnIndex(cname3));
  383. binder.addMedia(id, media_data);
  384. } while (cursor.moveToNext());
  385. binder.setPosition(position);
  386. cursor.moveToPosition(position);
  387. } finally {
  388. binder.play();
  389. }
  390. return true;
  391. }
  392. } catch (RemoteException e) {
  393. }
  394. return false;
  395. }
  396. /***
  397. * 曲を即時再生でかつ履歴は残す
  398. *
  399. * @param binder
  400. * @param id
  401. * @param duration
  402. * @param data
  403. */
  404. public static void playMedia(IMediaPlayerService binder, long id, String data) {
  405. try {
  406. String cntkey = SystemConsts.CONTENTSKEY_MEDIA + id;
  407. binder.setContentsKey(cntkey);
  408. binder.lockUpdateToPlay();
  409. try {
  410. binder.clearcut();
  411. binder.addMedia(id, data);
  412. } finally {
  413. binder.play();
  414. }
  415. } catch (RemoteException e) {
  416. }
  417. }
  418. public static long playGenres(IMediaPlayerService binder,
  419. BaseActivity activity, String genres_id) {
  420. long ret = -1;
  421. Cursor cursor = null;
  422. try {
  423. String cntkey = SystemConsts.CONTENTSKEY_GENRES + genres_id;
  424. cursor = activity.getContentResolver().query(
  425. MediaConsts.GENRES_MEMBER_CONTENT_URI, null,
  426. MediaConsts.AudioGenresMember.GENRE_ID + " = ?",
  427. new String[] { genres_id },
  428. MediaConsts.AudioGenresMember.DEFAULT_SORT_ORDER);
  429. if (cursor != null && cursor.moveToFirst()) {
  430. binder.setContentsKey(cntkey);
  431. binder.lockUpdateToPlay();
  432. try {
  433. activity.showProgressBar();
  434. binder.clearcut();
  435. String audio_id_col = MediaConsts.AudioGenresMember._ID;
  436. int audio_id_index = cursor
  437. .getColumnIndex(MediaConsts.AudioGenresMember.AUDIO_ID);
  438. if (audio_id_index != -1) {
  439. audio_id_col = MediaConsts.AudioGenresMember.AUDIO_ID;
  440. }
  441. do {
  442. long id = cursor.getLong(cursor
  443. .getColumnIndex(audio_id_col));
  444. String media_data = cursor
  445. .getString(cursor
  446. .getColumnIndex(MediaConsts.AudioGenresMember.DATA));
  447. if (ret == -1) {
  448. ret = id;
  449. }
  450. binder.addMedia(id, media_data);
  451. } while (cursor.moveToNext());
  452. } finally {
  453. binder.play();
  454. activity.hideProgressBar();
  455. }
  456. }
  457. } catch (RemoteException e) {
  458. } finally {
  459. if (cursor != null) {
  460. cursor.close();
  461. }
  462. }
  463. return ret;
  464. }
  465. public static long playArtist(IMediaPlayerService binder,
  466. BaseActivity activity, String artist_key) {
  467. long ret = -1;
  468. Cursor cursor = null;
  469. try {
  470. String cntkey = SystemConsts.CONTENTSKEY_ARTIST + artist_key;
  471. cursor = activity.getContentResolver().query(
  472. MediaConsts.MEDIA_CONTENT_URI,
  473. new String[]{MediaConsts.AudioMedia._ID},
  474. MediaConsts.AudioMedia.ARTIST_KEY + " = ?",
  475. new String[] { artist_key },
  476. MediaConsts.AudioMedia.ALBUM + ","
  477. + MediaConsts.AudioMedia.TRACK);
  478. if (cursor != null && cursor.moveToFirst()) {
  479. binder.setContentsKey(cntkey);
  480. binder.lockUpdateToPlay();
  481. try {
  482. activity.showProgressBar();
  483. binder.clearcut();
  484. do {
  485. long id = cursor.getLong(cursor
  486. .getColumnIndex(MediaConsts.AudioMedia._ID));
  487. String media_data = cursor
  488. .getString(cursor
  489. .getColumnIndex(MediaConsts.AudioMedia.DATA));
  490. if (ret == -1) {
  491. ret = id;
  492. }
  493. binder.addMedia(id, media_data);
  494. } while (cursor.moveToNext());
  495. } finally {
  496. activity.hideProgressBar();
  497. binder.play();
  498. }
  499. }
  500. } catch (RemoteException e) {
  501. } finally {
  502. if (cursor != null) {
  503. cursor.close();
  504. }
  505. }
  506. return ret;
  507. }
  508. public static long playArtistTitle(IMediaPlayerService binder,
  509. BaseActivity activity, String artist) {
  510. long ret = -1;
  511. Cursor cursor = null;
  512. try {
  513. String cntkey = SystemConsts.CONTENTSKEY_ARTIST_TITLE + artist;
  514. cursor = activity.getContentResolver().query(
  515. MediaConsts.MEDIA_CONTENT_URI,
  516. new String[]{MediaConsts.AudioMedia._ID},
  517. MediaConsts.AudioMedia.ARTIST + " like '%' || ? || '%'",
  518. new String[] { artist },
  519. MediaConsts.AudioMedia.ALBUM + ","
  520. + MediaConsts.AudioMedia.TRACK);
  521. if (cursor != null && cursor.moveToFirst()) {
  522. binder.setContentsKey(cntkey);
  523. binder.lockUpdateToPlay();
  524. try {
  525. activity.showProgressBar();
  526. binder.clearcut();
  527. do {
  528. long id = cursor.getLong(cursor
  529. .getColumnIndex(MediaConsts.AudioMedia._ID));
  530. String media_data = cursor
  531. .getString(cursor
  532. .getColumnIndex(MediaConsts.AudioMedia.DATA));
  533. if (ret == -1) {
  534. ret = id;
  535. }
  536. binder.addMedia(id, media_data);
  537. } while (cursor.moveToNext());
  538. } finally {
  539. binder.play();
  540. activity.hideProgressBar();
  541. }
  542. }
  543. } catch (RemoteException e) {
  544. } finally {
  545. if (cursor != null) {
  546. cursor.close();
  547. }
  548. }
  549. return ret;
  550. }
  551. public static boolean isSDCard(Context context) {
  552. MyPreferenceManager pref = new MyPreferenceManager(context);
  553. return isSDCard(pref);
  554. }
  555. public static boolean isSDCard(MyPreferenceManager pref) {
  556. String contenturi = pref.getContentUri();
  557. return contenturi.equals(DeviceContentProvider.MEDIA_AUTHORITY);
  558. }
  559. public static boolean isNoCacheAction(Context context) {
  560. MyPreferenceManager pref = new MyPreferenceManager(context);
  561. return isNoCacheAction(pref);
  562. }
  563. public static boolean isNoCacheAction(MyPreferenceManager pref) {
  564. String contenturi = pref.getContentUri();
  565. return contenturi.equals(MediaConsts.AUTHORITY + ".ds.ampache");
  566. }
  567. public static Hashtable<String, String> getMedia(Context contxt,
  568. String[] projection, long id) {
  569. return getMedia(contxt, projection, id, new Hashtable<String, String>());
  570. }
  571. public static Hashtable<String, String> getMedia(Context contxt,
  572. String[] projection, long id, Hashtable<String, String> tbl) {
  573. Cursor cur = null;
  574. try {
  575. if (id > 0) {
  576. String[] prj = projection;
  577. if (prj == null) {
  578. prj = new String[] { AudioMedia.ALBUM,
  579. AudioMedia.ALBUM_KEY, AudioMedia.ARTIST,
  580. AudioMedia.TITLE, AudioMedia.DURATION };
  581. }
  582. cur = contxt.getContentResolver().query(
  583. ContentUris.withAppendedId(
  584. MediaConsts.MEDIA_CONTENT_URI, id), prj, null,
  585. null, null);
  586. if (cur != null && cur.moveToFirst()) {
  587. for (int i = 0; i < prj.length; i++) {
  588. String value = cur
  589. .getString(cur.getColumnIndex(prj[i]));
  590. if (value != null) {
  591. tbl.put(prj[i], value);
  592. }
  593. }
  594. }
  595. }
  596. return tbl;
  597. } finally {
  598. if (cur != null) {
  599. cur.close();
  600. }
  601. }
  602. }
  603. public static Hashtable<String, String> getMedia(Context contxt,
  604. String[] projection, String media_key) {
  605. return getMedia(contxt, projection, media_key, new Hashtable<String, String>());
  606. }
  607. public static Hashtable<String, String> getMedia(Context contxt,
  608. String[] projection, String media_key, Hashtable<String, String> tbl) {
  609. Cursor cur = null;
  610. try {
  611. if (media_key != null) {
  612. String[] prj = projection;
  613. if (prj == null) {
  614. prj = new String[] { AudioMedia.ALBUM,
  615. AudioMedia.ALBUM_KEY, AudioMedia.ARTIST,
  616. AudioMedia.TITLE, AudioMedia.DURATION };
  617. }
  618. cur = contxt.getContentResolver().query(
  619. MediaConsts.MEDIA_CONTENT_URI, prj,
  620. MediaConsts.AudioMedia.MEDIA_KEY + " = ?",
  621. new String[] { media_key }, null);
  622. if (cur != null && cur.moveToFirst()) {
  623. for (int i = 0; i < prj.length; i++) {
  624. String value = cur
  625. .getString(cur.getColumnIndex(prj[i]));
  626. if (value != null) {
  627. tbl.put(prj[i], value);
  628. }
  629. }
  630. }
  631. }
  632. return tbl;
  633. } finally {
  634. if (cur != null) {
  635. cur.close();
  636. }
  637. }
  638. }
  639. public static Cursor getMediaFile(Context contxt, long id, String path) {
  640. return contxt.getContentResolver().query(
  641. ContentUris.withAppendedId(MediaConsts.FOLDER_CONTENT_URI, id),
  642. null, MediaConsts.FileMedia.DATA + " = ?",
  643. new String[] { path }, null);
  644. }
  645. public static Hashtable<String, String> getAlbum(Context contxt,
  646. String[] projection, String album_key) {
  647. Hashtable<String, String> tbl = new Hashtable<String, String>();
  648. return getAlbum(contxt, projection, album_key, tbl);
  649. }
  650. public static Hashtable<String, String> getAlbum(Context contxt,
  651. String[] projection, String album_key, Hashtable<String, String> tbl) {
  652. Cursor cur = null;
  653. try {
  654. if (album_key != null) {
  655. String[] prj = projection;
  656. if (prj == null) {
  657. prj = new String[] { AudioAlbum.ALBUM,
  658. AudioAlbum.ALBUM_KEY, AudioAlbum.ARTIST };
  659. }
  660. cur = contxt.getContentResolver().query(
  661. MediaConsts.ALBUM_CONTENT_URI, prj,
  662. AudioAlbum.ALBUM_KEY + "= ?",
  663. new String[] { album_key }, null);
  664. if (cur != null && cur.moveToFirst()) {
  665. for (int i = 0; i < prj.length; i++) {
  666. String value = cur
  667. .getString(cur.getColumnIndex(prj[i]));
  668. if (value != null) {
  669. tbl.put(prj[i], value);
  670. }
  671. }
  672. }
  673. }
  674. return tbl;
  675. } finally {
  676. if (cur != null) {
  677. cur.close();
  678. }
  679. }
  680. }
  681. public static Hashtable<String, String> getArtist(Context contxt,
  682. String[] projection, String artist_key) {
  683. Hashtable<String, String> tbl = new Hashtable<String, String>();
  684. Cursor cur = null;
  685. try {
  686. if (artist_key != null) {
  687. String[] prj = projection;
  688. if (prj == null) {
  689. prj = new String[] { AudioArtist.ARTIST,
  690. AudioArtist.ARTIST_KEY };
  691. }
  692. cur = contxt.getContentResolver().query(
  693. MediaConsts.ARTIST_CONTENT_URI, prj,
  694. AudioArtist.ARTIST_KEY + "= ?",
  695. new String[] { artist_key }, null);
  696. if (cur != null && cur.moveToFirst()) {
  697. for (int i = 0; i < prj.length; i++) {
  698. String value = cur
  699. .getString(cur.getColumnIndex(prj[i]));
  700. if (value != null) {
  701. tbl.put(prj[i], value);
  702. }
  703. }
  704. }
  705. }
  706. return tbl;
  707. } finally {
  708. if (cur != null) {
  709. cur.close();
  710. }
  711. }
  712. }
  713. public static String getAlbumKey(Context contxt, String album) {
  714. Cursor cur = null;
  715. try {
  716. cur = contxt.getContentResolver().query(
  717. MediaConsts.ALBUM_CONTENT_URI,
  718. new String[] { AudioAlbum.ALBUM_KEY },
  719. AudioAlbum.ALBUM + " = ?", new String[] { album }, null);
  720. if (cur != null && cur.moveToFirst()) {
  721. String value = cur.getString(cur
  722. .getColumnIndex(AudioAlbum.ALBUM_KEY));
  723. return value;
  724. }
  725. return null;
  726. } finally {
  727. if (cur != null) {
  728. cur.close();
  729. }
  730. }
  731. }
  732. public static Hashtable<String, String> getMediaInfoForFavorite(
  733. Context context, long media_id) {
  734. Hashtable<String, String> tbl = new Hashtable<String, String>();
  735. Cursor cur = null;
  736. try {
  737. cur = context.getContentResolver().query(
  738. ContentUris.withAppendedId(MediaConsts.MEDIA_CONTENT_URI,
  739. media_id),
  740. new String[] { AudioMedia.ALBUM, AudioMedia.ARTIST,
  741. AudioMedia.TITLE, AudioMedia.DURATION }, null,
  742. null, null);
  743. if (cur != null && cur.moveToFirst()) {
  744. tbl.put(AudioMedia.ALBUM,
  745. cur.getString(cur.getColumnIndex(AudioMedia.ALBUM)));
  746. tbl.put(AudioMedia.ARTIST,
  747. cur.getString(cur.getColumnIndex(AudioMedia.ARTIST)));
  748. tbl.put(AudioMedia.DURATION,
  749. cur.getString(cur.getColumnIndex(AudioMedia.DURATION)));
  750. tbl.put(AudioMedia.TITLE,
  751. cur.getString(cur.getColumnIndex(AudioMedia.TITLE)));
  752. return tbl;
  753. }
  754. return null;
  755. } finally {
  756. if (cur != null) {
  757. cur.close();
  758. }
  759. }
  760. }
  761. public static String getArtistKey(Context contxt, String artist) {
  762. Cursor cur = null;
  763. try {
  764. cur = contxt.getContentResolver().query(
  765. MediaConsts.ARTIST_CONTENT_URI,
  766. new String[] { AudioArtist.ARTIST_KEY },
  767. AudioArtist.ARTIST + " = ?", new String[] { artist }, null);
  768. if (cur != null && cur.moveToFirst()) {
  769. String value = cur.getString(cur
  770. .getColumnIndex(AudioArtist.ARTIST_KEY));
  771. return value;
  772. }
  773. return null;
  774. } finally {
  775. if (cur != null) {
  776. cur.close();
  777. }
  778. }
  779. }
  780. public static void lastfmLove(Context context, String title, String artist,
  781. String album, String duration) {
  782. try {
  783. Intent i = new Intent(SystemConsts.LASTFM_LOVE);
  784. i.putExtra("artist", artist);
  785. i.putExtra("track", title);
  786. if (album != null)
  787. i.putExtra("album", album);
  788. i.putExtra("duration", duration);
  789. context.sendBroadcast(i);
  790. } catch (Exception e) {
  791. Logger.e("lastfm love", e);
  792. }
  793. }
  794. public static void lastfmBan(Context context, String title, String artist,
  795. String album, String duration) {
  796. try {
  797. Intent i = new Intent(SystemConsts.LASTFM_BAN);
  798. i.putExtra("artist", artist);
  799. i.putExtra("track", title);
  800. if (album != null)
  801. i.putExtra("album", album);
  802. i.putExtra("duration", duration);
  803. context.sendBroadcast(i);
  804. } catch (Exception e) {
  805. Logger.e("lastfm love", e);
  806. }
  807. }
  808. public static ArrayList<String> getAlbumsArtist(Context context,
  809. MyPreferenceManager pref, String artist, String artist_key,
  810. int nalbums) {
  811. Cursor cur = null;
  812. Cursor cur2 = null;
  813. ArrayList<String> albumkes = new ArrayList<String>();
  814. ArrayList<String> tbl = new ArrayList<String>();
  815. try {
  816. String where;
  817. String[] whereArgs;
  818. if (ContentsUtils.isNoCacheAction(pref)) {
  819. where = AudioArtist.ARTIST_KEY + " = ?";
  820. whereArgs = new String[] { artist_key };
  821. } else {
  822. where = AudioAlbum.ARTIST + " = ?";
  823. whereArgs = new String[] { artist };
  824. }
  825. cur = context.getContentResolver().query(
  826. MediaConsts.ALBUM_CONTENT_URI,
  827. new String[] { AudioMedia.ALBUM_KEY }, where, whereArgs,
  828. null);
  829. if (cur != null && cur.moveToFirst()) {
  830. do {
  831. String album_key = cur.getString(cur
  832. .getColumnIndex(AudioAlbum.ALBUM_KEY));
  833. albumkes.add(album_key);
  834. } while (cur.moveToNext());
  835. }
  836. // コンピレーションアルバムの可能性、曲から検索し該当するアルバムを追加する
  837. if (nalbums > 0 && albumkes.size() < nalbums) {
  838. cur2 = context.getContentResolver().query(
  839. MediaConsts.MEDIA_CONTENT_URI,
  840. new String[] { AudioMedia.ALBUM_KEY },
  841. AudioArtist.ARTIST_KEY + " = ?",
  842. new String[] { artist_key }, null);
  843. if (cur2 != null && cur2.moveToFirst()) {
  844. do {
  845. String album_key = cur2.getString(cur2
  846. .getColumnIndex(AudioMedia.ALBUM_KEY));
  847. if (tbl.indexOf(album_key) == -1) {
  848. albumkes.add(album_key);
  849. }
  850. } while (cur2.moveToNext());
  851. }
  852. }
  853. return albumkes;
  854. } finally {
  855. if (cur != null) {
  856. cur.close();
  857. }
  858. if (cur2 != null) {
  859. cur2.close();
  860. }
  861. }
  862. }
  863. public static void clearArtistCache(Context context, String artistKey) {
  864. Cursor cursor = null;
  865. try {
  866. cursor = context.getContentResolver().query(
  867. MediaConsts.MEDIA_CONTENT_URI,
  868. null,
  869. MediaConsts.AudioMedia.ARTIST_KEY + " = ?",
  870. new String[] { artistKey },
  871. MediaConsts.AudioMedia.ALBUM + ","
  872. + MediaConsts.AudioMedia.TRACK);
  873. if (cursor != null && cursor.moveToFirst()) {
  874. do {
  875. String data = cursor
  876. .getString(cursor
  877. .getColumnIndex(MediaConsts.AudioMedia.DATA));
  878. File file = StreamCacherServer.getCacheFile(
  879. context, data);
  880. if (file != null && file.exists()) {
  881. file.delete();
  882. }
  883. } while (cursor.moveToNext());
  884. }
  885. } finally {
  886. if (cursor != null) {
  887. cursor.close();
  888. }
  889. }
  890. }
  891. public static void clearAlbumCache(Context context, String albumKey) {
  892. Cursor cursor = null;
  893. try {
  894. cursor = context.getContentResolver().query(
  895. MediaConsts.MEDIA_CONTENT_URI,
  896. new String[] { MediaConsts.AudioMedia.DATA },
  897. MediaConsts.AudioMedia.ALBUM_KEY + " = ?",
  898. new String[] { albumKey }, MediaConsts.AudioMedia.TRACK);
  899. if (cursor != null && cursor.moveToFirst()) {
  900. do {
  901. String data = cursor.getString(cursor
  902. .getColumnIndex(MediaConsts.AudioMedia.DATA));
  903. File file = StreamCacherServer.getCacheFile(context, data);
  904. if (file != null && file.exists()) {
  905. file.delete();
  906. }
  907. } while (cursor.moveToNext());
  908. }
  909. } finally {
  910. if (cursor != null) {
  911. cursor.close();
  912. }
  913. }
  914. }
  915. public static void clearMediaCache(Context context, String data) {
  916. File file = StreamCacherServer.getCacheFile(context, data);
  917. if (file != null && file.exists()) {
  918. file.delete();
  919. }
  920. }
  921. }