OSDN Git Service

am 5edd0cbd: Fix build.
[android-x86/packages-apps-Music.git] / src / com / android / music / QueryBrowserActivity.java
index b0e8dac..2b883a0 100644 (file)
@@ -92,15 +92,16 @@ implements MusicUtils.Defs, ServiceConnection
         registerReceiver(mScanListener, f);
         
         Intent intent = getIntent();
+        String action = intent != null ? intent.getAction() : null;
         
-        if (intent.getAction().equals(Intent.ACTION_VIEW)) {
+        if (Intent.ACTION_VIEW.equals(action)) {
             // this is something we got from the search bar
             Uri uri = intent.getData();
             String path = uri.toString();
             if (path.startsWith("content://media/external/audio/media/")) {
                 // This is a specific file
                 String id = uri.getLastPathSegment();
-                int [] list = new int[] { Integer.valueOf(id) };
+                long [] list = new long[] { Long.valueOf(id) };
                 MusicUtils.playAll(this, list, 0);
                 finish();
                 return;
@@ -122,7 +123,30 @@ implements MusicUtils.Defs, ServiceConnection
                 return;
             }
         }
+
         mFilterString = intent.getStringExtra(SearchManager.QUERY);
+        if (MediaStore.INTENT_ACTION_MEDIA_SEARCH.equals(action)) {
+            String focus = intent.getStringExtra(MediaStore.EXTRA_MEDIA_FOCUS);
+            String artist = intent.getStringExtra(MediaStore.EXTRA_MEDIA_ARTIST);
+            String album = intent.getStringExtra(MediaStore.EXTRA_MEDIA_ALBUM);
+            String title = intent.getStringExtra(MediaStore.EXTRA_MEDIA_TITLE);
+            if (focus != null) {
+                if (focus.startsWith("audio/") && title != null) {
+                    mFilterString = title;
+                } else if (focus.equals(MediaStore.Audio.Albums.ENTRY_CONTENT_TYPE)) {
+                    if (album != null) {
+                        mFilterString = album;
+                        if (artist != null) {
+                            mFilterString = mFilterString + " " + artist;
+                        }
+                    }
+                } else if (focus.equals(MediaStore.Audio.Artists.ENTRY_CONTENT_TYPE)) {
+                    if (artist != null) {
+                        mFilterString = artist;
+                    }
+                }
+            }
+        }
 
         setContentView(R.layout.query_activity);
         mTrackList = getListView();
@@ -174,18 +198,19 @@ implements MusicUtils.Defs, ServiceConnection
     public void onDestroy() {
         MusicUtils.unbindFromService(this);
         unregisterReceiver(mScanListener);
-        super.onDestroy();
+        // If we have an adapter and didn't send it off to another activity yet, we should
+        // close its cursor, which we do by assigning a null cursor to it. Doing this
+        // instead of closing the cursor directly keeps the framework from accessing
+        // the closed cursor later.
         if (!mAdapterSent && mAdapter != null) {
-            Cursor c = mAdapter.getCursor();
-            if (c != null) {
-                c.close();
-            }
+            mAdapter.changeCursor(null);
         }
         // Because we pass the adapter to the next activity, we need to make
         // sure it doesn't keep a reference to this activity. We can do this
         // by clearing its DatasetObservers, which setListAdapter(null) does.
         setListAdapter(null);
         mAdapter = null;
+        super.onDestroy();
     }
     
     /*
@@ -203,7 +228,9 @@ implements MusicUtils.Defs, ServiceConnection
     private Handler mReScanHandler = new Handler() {
         @Override
         public void handleMessage(Message msg) {
-            getQueryCursor(mAdapter.getQueryHandler(), null);
+            if (mAdapter != null) {
+                getQueryCursor(mAdapter.getQueryHandler(), null);
+            }
             // if the query results in a null cursor, onQueryComplete() will
             // call init(), which will post a delayed message to this handler
             // in order to try again.
@@ -224,7 +251,10 @@ implements MusicUtils.Defs, ServiceConnection
     }
     
     public void init(Cursor c) {
-        
+
+        if (mAdapter == null) {
+            return;
+        }
         mAdapter.changeCursor(c);
 
         if (mQueryCursor == null) {
@@ -250,16 +280,18 @@ implements MusicUtils.Defs, ServiceConnection
         
         if ("artist".equals(selectedType)) {
             Intent intent = new Intent(Intent.ACTION_PICK);
+            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
             intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/album");
             intent.putExtra("artist", Long.valueOf(id).toString());
             startActivity(intent);
         } else if ("album".equals(selectedType)) {
             Intent intent = new Intent(Intent.ACTION_PICK);
+            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
             intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/track");
             intent.putExtra("album", Long.valueOf(id).toString());
             startActivity(intent);
         } else if (position >= 0 && id >= 0){
-            int [] list = new int[] { (int) id };
+            long [] list = new long[] { id };
             MusicUtils.playAll(this, list, 0);
         } else {
             Log.e("QueryBrowser", "invalid position/id: " + position + "/" + id);
@@ -419,6 +451,10 @@ implements MusicUtils.Defs, ServiceConnection
         }
         @Override
         public void changeCursor(Cursor cursor) {
+            if (mActivity.isFinishing() && cursor != null) {
+                cursor.close();
+                cursor = null;
+            }
             if (cursor != mActivity.mQueryCursor) {
                 mActivity.mQueryCursor = cursor;
                 super.changeCursor(cursor);