Remove MediaView in favor of TextView-based layouts
This allows for RTL language support and will help with library display customization
This commit is contained in:
1 parent
35d64a1c9d
commit
2699be1cdb
23 files changed
+666
-623
No files matched your search
Binary file not shown.
Binary file not shown.
|
Before Width: | Height: | Size: 522 B After Width: | Height: | Size: 547 B |
Binary file not shown.
|
Before Width: | Height: | Size: 481 B After Width: | Height: | Size: 503 B |
Binary file not shown.
|
Before Width: | Height: | Size: 668 B After Width: | Height: | Size: 680 B |
@@ -34,7 +34,7 @@ THE SOFTWARE.
|
||||
android:scaleType="fitCenter"
|
||||
android:contentDescription="@string/cover_art" />
|
||||
<LinearLayout
|
||||
android:id="@+id/text"
|
||||
android:id="@+id/text_layout"
|
||||
android:layout_width="0px"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
|
||||
@@ -34,7 +34,7 @@ THE SOFTWARE.
|
||||
android:scaleType="fitCenter"
|
||||
android:contentDescription="@string/cover_art" />
|
||||
<LinearLayout
|
||||
android:id="@+id/text"
|
||||
android:id="@+id/text_layout"
|
||||
android:layout_width="0px"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (C) 2012 Christopher Eby <kreed@kreed.org>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
-->
|
||||
<TextView
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:background="@drawable/selectable_item_bg"
|
||||
android:maxLines="2"
|
||||
android:textColor="#ffff"
|
||||
android:gravity="left|center_vertical"
|
||||
android:padding="3dip"
|
||||
android:minHeight="44dip" />
|
||||
@@ -0,0 +1,51 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (C) 2012 Christopher Eby <kreed@kreed.org>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
-->
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="horizontal">
|
||||
<TextView
|
||||
android:id="@+id/text"
|
||||
android:longClickable="true"
|
||||
android:background="@drawable/selectable_item_bg"
|
||||
android:maxLines="2"
|
||||
android:padding="3dip"
|
||||
android:textColor="#ffff"
|
||||
android:gravity="left|center_vertical"
|
||||
android:layout_width="0px"
|
||||
android:layout_height="44dip"
|
||||
android:layout_weight="1" />
|
||||
<View
|
||||
android:id="@+id/divider"
|
||||
android:layout_width="1dip"
|
||||
android:layout_height="fill_parent"
|
||||
android:background="@color/divider_color" />
|
||||
<ImageView
|
||||
android:id="@+id/arrow"
|
||||
android:background="@drawable/selectable_item_bg"
|
||||
android:longClickable="true"
|
||||
android:scaleType="center"
|
||||
android:src="@drawable/arrow"
|
||||
android:layout_width="44dip"
|
||||
android:layout_height="44dip"
|
||||
android:contentDescription="@string/expand" />
|
||||
</LinearLayout>
|
||||
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (C) 2012 Christopher Eby <kreed@kreed.org>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
-->
|
||||
<ListView
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"
|
||||
android:divider="@color/divider_color"
|
||||
android:dividerHeight="1dip"
|
||||
android:listSelector="@drawable/selectable_item_bg"
|
||||
android:fastScrollEnabled="true"
|
||||
android:fastScrollAlwaysVisible="true"
|
||||
android:scrollbarStyle="outsideInset" />
|
||||
@@ -20,9 +20,11 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
-->
|
||||
<merge xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<org.kreed.vanilla.DragListView
|
||||
android:id="@+id/playlist"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent" />
|
||||
</merge>
|
||||
<org.kreed.vanilla.DragListView
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/playlist"
|
||||
android:fastScrollEnabled="true"
|
||||
android:divider="@null"
|
||||
android:dividerHeight="1dip"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent" />
|
||||
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (C) 2012 Christopher Eby <kreed@kreed.org>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
-->
|
||||
<org.kreed.vanilla.DragTextView
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:padding="3dip"
|
||||
android:minHeight="44dip"
|
||||
android:singleLine="true"
|
||||
android:textColor="#ffff"
|
||||
android:gravity="left|center_vertical" />
|
||||
@@ -33,6 +33,8 @@ THE SOFTWARE.
|
||||
<style name="AlertDialogItem">
|
||||
<item name="android:textColor">@android:color/primary_text_light</item>
|
||||
</style>
|
||||
<!-- Color used in LibraryActivity list dividers -->
|
||||
<color name="divider_color">#f444</color>
|
||||
<string-array name="entry_values">
|
||||
<!-- Note: even if this was an integer-array, these would be converted
|
||||
to strings anyway.
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.kreed.vanilla;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.Gravity;
|
||||
@@ -28,14 +29,13 @@ import android.view.WindowManager;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.ListView;
|
||||
import android.os.Handler;
|
||||
|
||||
/**
|
||||
* A ListView that supports dragging to reorder its elements.
|
||||
*
|
||||
* This implementation has some restrictions:
|
||||
* Footers are unsupported
|
||||
* All non-header views must be MediaViews of uniform height
|
||||
* All non-header views must have the same height
|
||||
* The adapter must be a PlaylistAdapter
|
||||
*
|
||||
* Dragging disabled by default. Enable it with
|
||||
@@ -45,19 +45,55 @@ import android.os.Handler;
|
||||
* HACKY. : /
|
||||
*/
|
||||
public class DragListView extends ListView implements Handler.Callback {
|
||||
/**
|
||||
* Sent to scroll the list up or down when the dragged view is near the
|
||||
* top or bottom of the list.
|
||||
*/
|
||||
private static final int MSG_SCROLL = 0;
|
||||
|
||||
/**
|
||||
* Height of each row in dip.
|
||||
*/
|
||||
private static final int ROW_HEIGHT = 44;
|
||||
/**
|
||||
* Padding for each row in dip.
|
||||
*/
|
||||
private static final int PADDING = 3;
|
||||
/**
|
||||
* Background color of row while it is being dragged.
|
||||
*/
|
||||
public static final int DRAG_COLOR = 0xff005500;
|
||||
/**
|
||||
* A handler running on the UI thread.
|
||||
*/
|
||||
private final Handler mHandler = new Handler(this);
|
||||
/**
|
||||
* The system window manager instance.
|
||||
*/
|
||||
private WindowManager mWindowManager;
|
||||
/**
|
||||
* The adapter that will be called to move/remove rows.
|
||||
*/
|
||||
private PlaylistAdapter mAdapter;
|
||||
|
||||
/**
|
||||
* True to allow dragging; false otherwise.
|
||||
*/
|
||||
private boolean mEditable;
|
||||
|
||||
/**
|
||||
* Scaled height of each row in pixels.
|
||||
*/
|
||||
private final int mRowHeight;
|
||||
/**
|
||||
* The view that is actually dragged around during a drag. (The original
|
||||
* view is hidden).
|
||||
*/
|
||||
private ImageView mDragView;
|
||||
/**
|
||||
* A copy of the dragged row's scrolling cache that is shown in mDragView.
|
||||
*/
|
||||
private Bitmap mDragBitmap;
|
||||
private WindowManager mWindowManager;
|
||||
/**
|
||||
* Window params for the drag view window. Used to move the window around.
|
||||
*/
|
||||
private WindowManager.LayoutParams mWindowParams;
|
||||
/**
|
||||
* At which position is the item currently being dragged. Note that this
|
||||
@@ -77,21 +113,34 @@ public class DragListView extends ListView implements Handler.Callback {
|
||||
* view.
|
||||
*/
|
||||
private int mYOffset;
|
||||
/**
|
||||
* The height of the view being dragged.
|
||||
*/
|
||||
private int mDragHeight;
|
||||
/**
|
||||
* The y coordinate of the top of the drag view after the last motion
|
||||
* event.
|
||||
*/
|
||||
private int mLastMotionY;
|
||||
/**
|
||||
* Default padding for rows.
|
||||
*/
|
||||
private final int mPadding;
|
||||
|
||||
public DragListView(Context context, AttributeSet attrs)
|
||||
{
|
||||
super(context, attrs);
|
||||
|
||||
float density = context.getResources().getDisplayMetrics().density;
|
||||
mPadding = (int)(PADDING * density);
|
||||
mRowHeight = (int)(ROW_HEIGHT * density);
|
||||
}
|
||||
|
||||
/**
|
||||
* This should be called instead of
|
||||
* {@link ListView#setAdapter(android.widget.ListAdapter)}.
|
||||
* DragListView requires a PlaylistAdapter to handle move/remove callbacks
|
||||
* from dragging.
|
||||
*
|
||||
* @param adapter The adapter to use. Will be passed to
|
||||
* {@link ListView#setAdapter(android.widget.ListAdapter)}.
|
||||
*/
|
||||
public void setAdapter(PlaylistAdapter adapter)
|
||||
{
|
||||
super.setAdapter(adapter);
|
||||
@@ -121,7 +170,7 @@ public class DragListView extends ListView implements Handler.Callback {
|
||||
stopDragging();
|
||||
|
||||
int x = (int)ev.getX();
|
||||
// The left half of the item is the grabber for dragging the item
|
||||
// The left quarter of the item is the grabber for dragging the item
|
||||
if (x < getWidth() / 4) {
|
||||
int item = pointToPosition(x, (int)ev.getY());
|
||||
if (item != AdapterView.INVALID_POSITION && item >= getHeaderViewsCount()) {
|
||||
@@ -174,6 +223,8 @@ public class DragListView extends ListView implements Handler.Callback {
|
||||
params.height = 0;
|
||||
view.setLayoutParams(params);
|
||||
view.setVisibility(View.VISIBLE);
|
||||
int padding = mPadding;
|
||||
view.setPadding(padding, padding, padding, padding);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,17 +253,24 @@ public class DragListView extends ListView implements Handler.Callback {
|
||||
View dragSrcView = getChildAt(mSrcDragPos - firstVisibile);
|
||||
|
||||
int start = firstVisibile < headerCount ? headerCount - firstVisibile : 0;
|
||||
for (int i = start; i != childCount; ++i) {
|
||||
MediaView view = (MediaView)getChildAt(i);
|
||||
int padding = mPadding;
|
||||
int rowHeight = mRowHeight;
|
||||
int nextHeight = rowHeight;
|
||||
|
||||
int height = 0;
|
||||
for (int i = start; i != childCount; ++i) {
|
||||
View view = getChildAt(i);
|
||||
|
||||
int height = nextHeight;
|
||||
nextHeight = rowHeight;
|
||||
int visibility = View.VISIBLE;
|
||||
boolean gravity = true;
|
||||
int paddingBottom = padding;
|
||||
int paddingTop = padding;
|
||||
|
||||
if (view == dragSrcView) {
|
||||
if (mDragPos == mSrcDragPos) {
|
||||
// hovering over the original location: show empty space
|
||||
visibility = View.INVISIBLE;
|
||||
height += 1;
|
||||
} else {
|
||||
// not hovering over it: show nothing
|
||||
// Ideally the item would be completely gone, but neither
|
||||
@@ -220,19 +278,20 @@ public class DragListView extends ListView implements Handler.Callback {
|
||||
// has the desired effect.
|
||||
height = 1;
|
||||
}
|
||||
nextHeight -= 1;
|
||||
} else if (i == childNum) {
|
||||
// hovering over this row; expand it to "make room" for the
|
||||
// dragged item
|
||||
height = view.getPreferredHeight() * 2;
|
||||
paddingTop += height;
|
||||
height *= 2;
|
||||
} else if (childNum == childCount && i == childCount - 1) {
|
||||
// hovering over the bottom of the list: we need to show the
|
||||
// expanded area at the bottom of the view, so set the gravity
|
||||
// to top
|
||||
height = view.getPreferredHeight() * 2;
|
||||
gravity = false;
|
||||
// hovering over the bottom of the list: we need to "make room"
|
||||
// at the bottom
|
||||
paddingBottom += height;
|
||||
height *= 2;
|
||||
}
|
||||
|
||||
view.setBottomGravity(gravity);
|
||||
view.setPadding(padding, paddingTop, padding, paddingBottom);
|
||||
view.setVisibility(visibility);
|
||||
ViewGroup.LayoutParams params = view.getLayoutParams();
|
||||
params.height = height;
|
||||
@@ -252,7 +311,7 @@ public class DragListView extends ListView implements Handler.Callback {
|
||||
// This assumes uniform height for all non-header rows
|
||||
int firstVisible = getFirstVisiblePosition();
|
||||
int topPos = Math.max(getHeaderViewsCount(), firstVisible);
|
||||
int dragHeight = mDragHeight;
|
||||
int dragHeight = mRowHeight;
|
||||
View view = getChildAt(topPos - firstVisible);
|
||||
int viewMiddle = view.getTop() + dragHeight / 2;
|
||||
int dragPos = Math.min(getCount() - 1, topPos + Math.max(0, y - viewMiddle + dragHeight) / dragHeight);
|
||||
@@ -302,11 +361,13 @@ public class DragListView extends ListView implements Handler.Callback {
|
||||
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
|
||||
mWindowParams.windowAnimations = 0;
|
||||
|
||||
int color = item.getDrawingCacheBackgroundColor();
|
||||
item.setDrawingCacheBackgroundColor(0xff005500);
|
||||
item.buildDrawingCache();
|
||||
// Create a copy of the drawing cache so that it does not get recycled
|
||||
// by the framework when the list tries to clean up memory
|
||||
Bitmap bitmap = Bitmap.createBitmap(item.getDrawingCache());
|
||||
item.setDrawingCacheBackgroundColor(color);
|
||||
item.destroyDrawingCache();
|
||||
mDragBitmap = bitmap;
|
||||
|
||||
@@ -319,7 +380,6 @@ public class DragListView extends ListView implements Handler.Callback {
|
||||
mWindowManager.addView(view, mWindowParams);
|
||||
mDragView = view;
|
||||
|
||||
mDragHeight = bitmap.getHeight();
|
||||
mSrcDragPos = row;
|
||||
// Force expansion on next motion event
|
||||
mDragPos = INVALID_POSITION;
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (C) 2012 Christopher Eby <kreed@kreed.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package org.kreed.vanilla;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.TextView;
|
||||
|
||||
/**
|
||||
* TextView that draws a divider line at the bottom.
|
||||
*
|
||||
* We draw the divider here rather than with ListView.setDivider() so we don't
|
||||
* have duplicate dividers when hiding a row for a drag.
|
||||
*/
|
||||
public class DragTextView extends TextView {
|
||||
private final Paint mPaint;
|
||||
|
||||
public DragTextView(Context context, AttributeSet attrs)
|
||||
{
|
||||
super(context, attrs);
|
||||
Paint paint = new Paint();
|
||||
paint.setColor(0xff444444);
|
||||
mPaint = paint;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDraw(Canvas canvas)
|
||||
{
|
||||
int height = getHeight();
|
||||
if (height <= 1)
|
||||
return;
|
||||
super.onDraw(canvas);
|
||||
if (getDrawingCacheBackgroundColor() != DragListView.DRAG_COLOR) {
|
||||
// only draw divider when not dragging
|
||||
float h = height - 1;
|
||||
canvas.drawLine(0, h, getWidth(), h, mPaint);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,15 +22,21 @@
|
||||
|
||||
package org.kreed.vanilla;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Environment;
|
||||
import android.os.FileObserver;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -39,7 +45,11 @@ import java.util.regex.Pattern;
|
||||
* A list adapter that provides a view of the filesystem. The active directory
|
||||
* is set through a {@link Limiter} and rows are displayed using MediaViews.
|
||||
*/
|
||||
public class FileSystemAdapter extends BaseAdapter implements LibraryAdapter {
|
||||
public class FileSystemAdapter
|
||||
extends BaseAdapter
|
||||
implements LibraryAdapter
|
||||
, View.OnClickListener
|
||||
{
|
||||
private static final Pattern SPACE_SPLIT = Pattern.compile("\\s+");
|
||||
private static final Pattern FILE_SEPARATOR = Pattern.compile(File.separator);
|
||||
|
||||
@@ -47,6 +57,10 @@ public class FileSystemAdapter extends BaseAdapter implements LibraryAdapter {
|
||||
* The owner LibraryActivity.
|
||||
*/
|
||||
final LibraryActivity mActivity;
|
||||
/**
|
||||
* A LayoutInflater to use.
|
||||
*/
|
||||
private final LayoutInflater mInflater;
|
||||
/**
|
||||
* The currently active limiter, set by a row expander being clicked.
|
||||
*/
|
||||
@@ -58,7 +72,7 @@ public class FileSystemAdapter extends BaseAdapter implements LibraryAdapter {
|
||||
/**
|
||||
* The folder icon shown for folder rows.
|
||||
*/
|
||||
private final Bitmap mFolderIcon;
|
||||
private final Drawable mFolderIcon;
|
||||
/**
|
||||
* The currently active filter, entered by the user from the search box.
|
||||
*/
|
||||
@@ -117,7 +131,8 @@ public class FileSystemAdapter extends BaseAdapter implements LibraryAdapter {
|
||||
{
|
||||
mActivity = activity;
|
||||
mLimiter = limiter;
|
||||
mFolderIcon = BitmapFactory.decodeResource(activity.getResources(), R.drawable.folder);
|
||||
mFolderIcon = activity.getResources().getDrawable(R.drawable.folder);
|
||||
mInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
if (limiter == null) {
|
||||
limiter = buildLimiter(Environment.getExternalStorageDirectory());
|
||||
}
|
||||
@@ -173,18 +188,40 @@ public class FileSystemAdapter extends BaseAdapter implements LibraryAdapter {
|
||||
return pos;
|
||||
}
|
||||
|
||||
private static class ViewHolder {
|
||||
public int id;
|
||||
public TextView text;
|
||||
public View divider;
|
||||
public ImageView arrow;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int pos, View convertView, ViewGroup parent)
|
||||
{
|
||||
MediaView view;
|
||||
View view;
|
||||
ViewHolder holder;
|
||||
|
||||
if (convertView == null) {
|
||||
view = new MediaView(mActivity, mFolderIcon, MediaView.sExpander);
|
||||
view = mInflater.inflate(R.layout.library_row_expandable, null);
|
||||
holder = new ViewHolder();
|
||||
holder.text = (TextView)view.findViewById(R.id.text);
|
||||
holder.divider = view.findViewById(R.id.divider);
|
||||
holder.arrow = (ImageView)view.findViewById(R.id.arrow);
|
||||
holder.text.setOnClickListener(this);
|
||||
holder.arrow.setOnClickListener(this);
|
||||
view.setTag(holder);
|
||||
} else {
|
||||
view = (MediaView)convertView;
|
||||
view = convertView;
|
||||
holder = (ViewHolder)view.getTag();
|
||||
}
|
||||
|
||||
File file = mFiles[pos];
|
||||
view.setData(pos, file.getName());
|
||||
view.setShowBitmaps(file.isDirectory());
|
||||
boolean isDirectory = file.isDirectory();
|
||||
holder.id = pos;
|
||||
holder.text.setText(file.getName());
|
||||
holder.divider.setVisibility(isDirectory ? View.VISIBLE : View.GONE);
|
||||
holder.arrow.setVisibility(isDirectory ? View.VISIBLE : View.GONE);
|
||||
holder.text.setCompoundDrawablesWithIntrinsicBounds(isDirectory ? mFolderIcon : null, null, null, null);
|
||||
return view;
|
||||
}
|
||||
|
||||
@@ -253,4 +290,38 @@ public class FileSystemAdapter extends BaseAdapter implements LibraryAdapter {
|
||||
mActivity.mPagerAdapter.postRequestRequery(FileSystemAdapter.this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Intent createData(View view)
|
||||
{
|
||||
ViewHolder holder = (ViewHolder)view.getTag();
|
||||
File file = mFiles[holder.id];
|
||||
|
||||
Intent intent = new Intent();
|
||||
intent.putExtra(LibraryAdapter.DATA_TYPE, MediaUtils.TYPE_FILE);
|
||||
intent.putExtra(LibraryAdapter.DATA_ID, (long)holder.id);
|
||||
intent.putExtra(LibraryAdapter.DATA_TITLE, holder.text.getText().toString());
|
||||
intent.putExtra(LibraryAdapter.DATA_EXPANDABLE, file.isDirectory());
|
||||
|
||||
String path;
|
||||
try {
|
||||
path = file.getCanonicalPath();
|
||||
} catch (IOException e) {
|
||||
path = file.getAbsolutePath();
|
||||
Log.e("VanillaMusic", "Failed to canonicalize path", e);
|
||||
}
|
||||
intent.putExtra(LibraryAdapter.DATA_FILE, path);
|
||||
return intent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View view)
|
||||
{
|
||||
Intent intent = createData((View)view.getParent());
|
||||
if (view.getId() == R.id.arrow) {
|
||||
mActivity.onItemExpanded(intent);
|
||||
} else {
|
||||
mActivity.onItemClicked(intent);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -130,7 +130,7 @@ public class FourLongWidget extends AppWidgetProvider {
|
||||
intent.setAction(Intent.ACTION_MAIN);
|
||||
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
|
||||
views.setOnClickPendingIntent(R.id.cover, pendingIntent);
|
||||
views.setOnClickPendingIntent(R.id.text, pendingIntent);
|
||||
views.setOnClickPendingIntent(R.id.text_layout, pendingIntent);
|
||||
|
||||
intent = new Intent(PlaybackService.ACTION_TOGGLE_PLAYBACK).setComponent(service);
|
||||
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
|
||||
|
||||
@@ -41,7 +41,6 @@ import android.support.v4.view.ViewPager;
|
||||
import android.text.Editable;
|
||||
import android.text.TextUtils;
|
||||
import android.text.TextWatcher;
|
||||
import android.util.Log;
|
||||
import android.view.ContextMenu;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.Menu;
|
||||
@@ -51,7 +50,6 @@ import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.ViewParent;
|
||||
import android.view.inputmethod.InputMethodManager;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.HorizontalScrollView;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.ImageView;
|
||||
@@ -62,7 +60,6 @@ import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
import com.viewpagerindicator.TabPageIndicator;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import junit.framework.Assert;
|
||||
|
||||
/**
|
||||
@@ -70,8 +67,7 @@ import junit.framework.Assert;
|
||||
*/
|
||||
public class LibraryActivity
|
||||
extends PlaybackActivity
|
||||
implements AdapterView.OnItemClickListener
|
||||
, TextWatcher
|
||||
implements TextWatcher
|
||||
, DialogInterface.OnClickListener
|
||||
, DialogInterface.OnDismissListener
|
||||
{
|
||||
@@ -161,7 +157,6 @@ public class LibraryActivity
|
||||
checkForLaunch(getIntent());
|
||||
}
|
||||
|
||||
MediaView.init(this);
|
||||
setContentView(R.layout.library_content);
|
||||
|
||||
mSearchBox = findViewById(R.id.search_box);
|
||||
@@ -245,7 +240,7 @@ public class LibraryActivity
|
||||
startActivity(new Intent(this, LibraryActivity.class));
|
||||
}
|
||||
mDefaultAction = Integer.parseInt(settings.getString("default_action_int", "0"));
|
||||
mLastActedId = -2;
|
||||
mLastActedId = LibraryAdapter.INVALID_ID;
|
||||
updateHeaders();
|
||||
}
|
||||
|
||||
@@ -368,13 +363,13 @@ public class LibraryActivity
|
||||
*/
|
||||
private void pickSongs(Intent intent, int action)
|
||||
{
|
||||
long id = intent.getLongExtra("id", -1);
|
||||
long id = intent.getLongExtra("id", LibraryAdapter.INVALID_ID);
|
||||
|
||||
boolean all = false;
|
||||
int mode = action;
|
||||
if (action == ACTION_PLAY_ALL || action == ACTION_ENQUEUE_ALL) {
|
||||
int type = mCurrentAdapter.getMediaType();
|
||||
boolean notPlayAllAdapter = type > MediaUtils.TYPE_SONG || id == MediaView.HEADER_ID;
|
||||
boolean notPlayAllAdapter = type > MediaUtils.TYPE_SONG || id == LibraryAdapter.HEADER_ID;
|
||||
if (mode == ACTION_ENQUEUE_ALL && notPlayAllAdapter) {
|
||||
mode = ACTION_ENQUEUE;
|
||||
} else if (mode == ACTION_PLAY_ALL && notPlayAllAdapter) {
|
||||
@@ -406,7 +401,7 @@ public class LibraryActivity
|
||||
private void expand(Intent intent)
|
||||
{
|
||||
int type = intent.getIntExtra("type", 1);
|
||||
long id = intent.getLongExtra("id", -1);
|
||||
long id = intent.getLongExtra("id", LibraryAdapter.INVALID_ID);
|
||||
int tab = mPagerAdapter.setLimiter(mPagerAdapter.mAdapters[type - 1].buildLimiter(id));
|
||||
if (tab == mViewPager.getCurrentItem())
|
||||
updateLimiterViews();
|
||||
@@ -423,20 +418,20 @@ public class LibraryActivity
|
||||
startActivity(new Intent(this, FullPlaybackActivity.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> list, View view, int pos, long id)
|
||||
/**
|
||||
* Called by LibraryAdapters when a row has been clicked.
|
||||
*
|
||||
* @param row The data for the row that was clicked.
|
||||
*/
|
||||
public void onItemClicked(Intent rowData)
|
||||
{
|
||||
MediaView mediaView = (MediaView)view;
|
||||
LibraryAdapter adapter = (LibraryAdapter)list.getAdapter();
|
||||
int action = mDefaultAction;
|
||||
if (action == ACTION_LAST_USED)
|
||||
action = mLastAction;
|
||||
if (mediaView.isRightBitmapPressed() || action == ACTION_EXPAND && mediaView.hasRightBitmap()) {
|
||||
if (adapter.getMediaType() == MediaUtils.TYPE_PLAYLIST)
|
||||
editPlaylist(mediaView.getMediaId(), mediaView.getTitle());
|
||||
else
|
||||
expand(createClickIntent(adapter, mediaView));
|
||||
} else if (id == mLastActedId) {
|
||||
|
||||
if (action == ACTION_EXPAND && rowData.getBooleanExtra(LibraryAdapter.DATA_EXPANDABLE, false)) {
|
||||
onItemExpanded(rowData);
|
||||
} else if (rowData.getLongExtra(LibraryAdapter.DATA_ID, LibraryAdapter.INVALID_ID) == mLastActedId) {
|
||||
openPlaybackActivity();
|
||||
} else if (action != ACTION_DO_NOTHING) {
|
||||
if (action == ACTION_EXPAND) {
|
||||
@@ -444,10 +439,24 @@ public class LibraryActivity
|
||||
// be expanded
|
||||
action = ACTION_PLAY;
|
||||
}
|
||||
pickSongs(createClickIntent(adapter, mediaView), action);
|
||||
pickSongs(rowData, action);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by LibraryAdapters when a row's expand arrow has been clicked.
|
||||
*
|
||||
* @param row The data for the row that was clicked.
|
||||
*/
|
||||
public void onItemExpanded(Intent rowData)
|
||||
{
|
||||
int type = rowData.getIntExtra(LibraryAdapter.DATA_TYPE, -1);
|
||||
if (type == MediaUtils.TYPE_PLAYLIST)
|
||||
editPlaylist(rowData);
|
||||
else
|
||||
expand(rowData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable editable)
|
||||
{
|
||||
@@ -550,34 +559,6 @@ public class LibraryActivity
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an intent based off of the media represented by the given view.
|
||||
*
|
||||
* @param adapter The adapter that owns the view.
|
||||
* @param view The MediaView to build from.
|
||||
*/
|
||||
private static Intent createClickIntent(LibraryAdapter adapter, MediaView view)
|
||||
{
|
||||
int type = adapter.getMediaType();
|
||||
long id = view.getMediaId();
|
||||
Intent intent = new Intent();
|
||||
intent.putExtra("type", type);
|
||||
intent.putExtra("id", id);
|
||||
intent.putExtra("title", view.getTitle());
|
||||
if (type == MediaUtils.TYPE_FILE) {
|
||||
File file = (File)adapter.getItem((int)id);
|
||||
String path;
|
||||
try {
|
||||
path = file.getCanonicalPath();
|
||||
} catch (IOException e) {
|
||||
path = file.getAbsolutePath();
|
||||
Log.e("VanillaMusic", "Failed to canonicalize path", e);
|
||||
}
|
||||
intent.putExtra("file", path);
|
||||
}
|
||||
return intent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a media query based off the data stored in the given intent.
|
||||
*
|
||||
@@ -597,11 +578,11 @@ public class LibraryActivity
|
||||
else
|
||||
projection = empty ? Song.EMPTY_PROJECTION : Song.FILLED_PROJECTION;
|
||||
|
||||
long id = intent.getLongExtra("id", -1);
|
||||
long id = intent.getLongExtra("id", LibraryAdapter.INVALID_ID);
|
||||
QueryTask query;
|
||||
if (type == MediaUtils.TYPE_FILE) {
|
||||
query = MediaUtils.buildFileQuery(intent.getStringExtra("file"), projection);
|
||||
} else if (all || id == MediaView.HEADER_ID) {
|
||||
} else if (all || id == LibraryAdapter.HEADER_ID) {
|
||||
query = ((MediaAdapter)mPagerAdapter.mAdapters[type - 1]).buildSongQuery(projection);
|
||||
query.setExtra(id);
|
||||
} else {
|
||||
@@ -617,53 +598,44 @@ public class LibraryActivity
|
||||
private static final int MENU_ADD_TO_PLAYLIST = 3;
|
||||
private static final int MENU_NEW_PLAYLIST = 4;
|
||||
private static final int MENU_DELETE = 5;
|
||||
private static final int MENU_EDIT = 6;
|
||||
private static final int MENU_RENAME_PLAYLIST = 7;
|
||||
private static final int MENU_SELECT_PLAYLIST = 8;
|
||||
private static final int MENU_PLAY_ALL = 9;
|
||||
private static final int MENU_ENQUEUE_ALL = 10;
|
||||
|
||||
@Override
|
||||
public void onCreateContextMenu(ContextMenu menu, View listView, ContextMenu.ContextMenuInfo absInfo)
|
||||
/**
|
||||
* Creates a context menu for an adapter row.
|
||||
*
|
||||
* @param menu The menu to create.
|
||||
* @param rowData Data for the adapter row.
|
||||
*/
|
||||
public void onCreateContextMenu(ContextMenu menu, Intent rowData)
|
||||
{
|
||||
if (!(listView instanceof ListView)) {
|
||||
super.onCreateContextMenu(menu, listView, absInfo);
|
||||
return;
|
||||
}
|
||||
|
||||
LibraryAdapter adapter = (LibraryAdapter)((ListView)listView).getAdapter();
|
||||
MediaView view = (MediaView)((AdapterView.AdapterContextMenuInfo)absInfo).targetView;
|
||||
|
||||
// Store view data in intent to avoid problems when the view data changes
|
||||
// as worked is performed in the background.
|
||||
Intent intent = createClickIntent(adapter, view);
|
||||
|
||||
boolean isHeader = view.getMediaId() == MediaView.HEADER_ID;
|
||||
int type = adapter.getMediaType();
|
||||
boolean isAllAdapter = type <= MediaUtils.TYPE_SONG;
|
||||
|
||||
if (isHeader)
|
||||
if (rowData.getLongExtra(LibraryAdapter.DATA_ID, LibraryAdapter.INVALID_ID) == LibraryAdapter.HEADER_ID) {
|
||||
menu.setHeaderTitle(getString(R.string.all_songs));
|
||||
else
|
||||
menu.setHeaderTitle(view.getTitle());
|
||||
menu.add(0, MENU_PLAY_ALL, 0, R.string.play_all).setIntent(rowData);
|
||||
menu.add(0, MENU_ENQUEUE_ALL, 0, R.string.enqueue_all).setIntent(rowData);
|
||||
menu.addSubMenu(0, MENU_ADD_TO_PLAYLIST, 0, R.string.add_to_playlist).getItem().setIntent(rowData);
|
||||
} else {
|
||||
int type = rowData.getIntExtra(LibraryAdapter.DATA_TYPE, -1);
|
||||
boolean isAllAdapter = type <= MediaUtils.TYPE_SONG;
|
||||
|
||||
if (!isHeader)
|
||||
menu.add(0, MENU_PLAY, 0, R.string.play).setIntent(intent);
|
||||
if (isAllAdapter)
|
||||
menu.add(0, MENU_PLAY_ALL, 0, R.string.play_all).setIntent(intent);
|
||||
if (!isHeader)
|
||||
menu.add(0, MENU_ENQUEUE, 0, R.string.enqueue).setIntent(intent);
|
||||
if (isAllAdapter)
|
||||
menu.add(0, MENU_ENQUEUE_ALL, 0, R.string.enqueue_all).setIntent(intent);
|
||||
if (type == MediaUtils.TYPE_PLAYLIST) {
|
||||
menu.add(0, MENU_RENAME_PLAYLIST, 0, R.string.rename).setIntent(intent);
|
||||
menu.add(0, MENU_EDIT, 0, R.string.edit).setIntent(intent);
|
||||
menu.setHeaderTitle(rowData.getStringExtra(LibraryAdapter.DATA_TITLE));
|
||||
menu.add(0, MENU_PLAY, 0, R.string.play).setIntent(rowData);
|
||||
if (isAllAdapter)
|
||||
menu.add(0, MENU_PLAY_ALL, 0, R.string.play_all).setIntent(rowData);
|
||||
menu.add(0, MENU_ENQUEUE, 0, R.string.enqueue).setIntent(rowData);
|
||||
if (isAllAdapter)
|
||||
menu.add(0, MENU_ENQUEUE_ALL, 0, R.string.enqueue_all).setIntent(rowData);
|
||||
if (type == MediaUtils.TYPE_PLAYLIST) {
|
||||
menu.add(0, MENU_RENAME_PLAYLIST, 0, R.string.rename).setIntent(rowData);
|
||||
menu.add(0, MENU_EXPAND, 0, R.string.edit).setIntent(rowData);
|
||||
} else if (rowData.getBooleanExtra(LibraryAdapter.DATA_EXPANDABLE, false)) {
|
||||
menu.add(0, MENU_EXPAND, 0, R.string.expand).setIntent(rowData);
|
||||
}
|
||||
menu.addSubMenu(0, MENU_ADD_TO_PLAYLIST, 0, R.string.add_to_playlist).getItem().setIntent(rowData);
|
||||
menu.add(0, MENU_DELETE, 0, R.string.delete).setIntent(rowData);
|
||||
}
|
||||
menu.addSubMenu(0, MENU_ADD_TO_PLAYLIST, 0, R.string.add_to_playlist).getItem().setIntent(intent);
|
||||
if (view.hasRightBitmap())
|
||||
menu.add(0, MENU_EXPAND, 0, R.string.expand).setIntent(intent);
|
||||
if (!isHeader)
|
||||
menu.add(0, MENU_DELETE, 0, R.string.delete).setIntent(intent);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -686,11 +658,11 @@ public class LibraryActivity
|
||||
/**
|
||||
* Open the playlist editor for the playlist with the given id.
|
||||
*/
|
||||
private void editPlaylist(long playlistId, String title)
|
||||
private void editPlaylist(Intent rowData)
|
||||
{
|
||||
Intent launch = new Intent(this, PlaylistActivity.class);
|
||||
launch.putExtra("playlist", playlistId);
|
||||
launch.putExtra("title", title);
|
||||
launch.putExtra("playlist", rowData.getLongExtra(LibraryAdapter.DATA_ID, LibraryAdapter.INVALID_ID));
|
||||
launch.putExtra("title", rowData.getStringExtra(LibraryAdapter.DATA_TITLE));
|
||||
startActivity(launch);
|
||||
}
|
||||
|
||||
@@ -789,9 +761,6 @@ public class LibraryActivity
|
||||
}
|
||||
break;
|
||||
}
|
||||
case MENU_EDIT:
|
||||
editPlaylist(intent.getLongExtra("id", 0), intent.getStringExtra("title"));
|
||||
break;
|
||||
case MENU_SELECT_PLAYLIST:
|
||||
mHandler.sendMessage(mHandler.obtainMessage(MSG_ADD_TO_PLAYLIST, intent));
|
||||
break;
|
||||
@@ -1038,7 +1007,7 @@ public class LibraryActivity
|
||||
public void onAdapterSelected(LibraryAdapter adapter)
|
||||
{
|
||||
mCurrentAdapter = adapter;
|
||||
mLastActedId = -2;
|
||||
mLastActedId = LibraryAdapter.INVALID_ID;
|
||||
updateLimiterViews();
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
||||
CompatHoneycomb.selectTab(this, mViewPager.getCurrentItem());
|
||||
|
||||
@@ -22,6 +22,8 @@
|
||||
|
||||
package org.kreed.vanilla;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.view.View;
|
||||
import android.widget.ListAdapter;
|
||||
|
||||
/**
|
||||
@@ -97,4 +99,39 @@ public interface LibraryAdapter extends ListAdapter {
|
||||
* Must be called on the UI thread.
|
||||
*/
|
||||
public void clear();
|
||||
|
||||
/**
|
||||
* Creates the row data used by LibraryActivity.
|
||||
*/
|
||||
public Intent createData(View row);
|
||||
|
||||
/**
|
||||
* Extra for row data: media id. type: long.
|
||||
*/
|
||||
public static final String DATA_ID = "id";
|
||||
/**
|
||||
* Special id for {@link #DATA_ID}: the row represented is a header view.
|
||||
*/
|
||||
public static final long HEADER_ID = -1;
|
||||
/**
|
||||
* Special id for {@link #DATA_ID}: invalid id.
|
||||
*/
|
||||
public static final long INVALID_ID = -2;
|
||||
/**
|
||||
* Extra for row data: media title. type: String.
|
||||
*/
|
||||
public static final String DATA_TITLE = "title";
|
||||
/**
|
||||
* Extra for row data: media type. type: int. One of MediaUtils.TYPE_*.
|
||||
*/
|
||||
public static final String DATA_TYPE = "type";
|
||||
/**
|
||||
* Extra for row data: canonical file path. type: String. Only present if
|
||||
* type is {@link MediaUtils.TYPE_FILE}.
|
||||
*/
|
||||
public static final String DATA_FILE = "file";
|
||||
/**
|
||||
* Extra for row data: if true, row has expander arrow. type: boolean.
|
||||
*/
|
||||
public static final String DATA_EXPANDABLE = "expandable";
|
||||
}
|
||||
@@ -22,9 +22,9 @@
|
||||
|
||||
package org.kreed.vanilla;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.database.ContentObserver;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
@@ -33,14 +33,25 @@ import android.os.Parcelable;
|
||||
import android.provider.MediaStore;
|
||||
import android.support.v4.view.PagerAdapter;
|
||||
import android.support.v4.view.ViewPager;
|
||||
import android.view.ContextMenu;
|
||||
import android.view.ContextMenu.ContextMenuInfo;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ListView;
|
||||
import android.widget.TextView;
|
||||
|
||||
/**
|
||||
* PagerAdapter that manages the library media ListViews.
|
||||
*/
|
||||
public class LibraryPagerAdapter extends PagerAdapter implements Handler.Callback, ViewPager.OnPageChangeListener {
|
||||
public class LibraryPagerAdapter
|
||||
extends PagerAdapter
|
||||
implements Handler.Callback
|
||||
, ViewPager.OnPageChangeListener
|
||||
, View.OnCreateContextMenuListener
|
||||
, AdapterView.OnItemClickListener
|
||||
{
|
||||
/**
|
||||
* The number of adapters/lists (determines array sizes).
|
||||
*/
|
||||
@@ -63,10 +74,6 @@ public class LibraryPagerAdapter extends PagerAdapter implements Handler.Callbac
|
||||
* Each adapter, in the same order as MediaUtils.TYPE_*.
|
||||
*/
|
||||
public final LibraryAdapter[] mAdapters = new LibraryAdapter[ADAPTER_COUNT];
|
||||
/**
|
||||
* The artist adapter instance, also stored at mAdapters[0].
|
||||
*/
|
||||
private MediaAdapter mArtistAdapter;
|
||||
/**
|
||||
* The album adapter instance, also stored at mAdapters[1].
|
||||
*/
|
||||
@@ -125,6 +132,9 @@ public class LibraryPagerAdapter extends PagerAdapter implements Handler.Callbac
|
||||
* song limiters.
|
||||
*/
|
||||
private String mHeaderText;
|
||||
private TextView mArtistHeader;
|
||||
private TextView mAlbumHeader;
|
||||
private TextView mSongHeader;
|
||||
/**
|
||||
* The current filter text, or null if none.
|
||||
*/
|
||||
@@ -163,28 +173,30 @@ public class LibraryPagerAdapter extends PagerAdapter implements Handler.Callbac
|
||||
|
||||
if (view == null) {
|
||||
LibraryActivity activity = mActivity;
|
||||
LayoutInflater inflater = activity.getLayoutInflater();
|
||||
LibraryAdapter adapter;
|
||||
TextView header = null;
|
||||
|
||||
switch (position) {
|
||||
case 0:
|
||||
adapter = mArtistAdapter = new MediaAdapter(activity, MediaUtils.TYPE_ARTIST, true, true, null);
|
||||
mArtistAdapter.setHeaderText(mHeaderText);
|
||||
adapter = new MediaAdapter(activity, MediaUtils.TYPE_ARTIST, null);
|
||||
mArtistHeader = header = (TextView)inflater.inflate(R.layout.library_row, null);
|
||||
break;
|
||||
case 1:
|
||||
adapter = mAlbumAdapter = new MediaAdapter(activity, MediaUtils.TYPE_ALBUM, true, true, mPendingAlbumLimiter);
|
||||
adapter = mAlbumAdapter = new MediaAdapter(activity, MediaUtils.TYPE_ALBUM, mPendingAlbumLimiter);
|
||||
mPendingAlbumLimiter = null;
|
||||
mAlbumAdapter.setHeaderText(mHeaderText);
|
||||
mAlbumHeader = header = (TextView)inflater.inflate(R.layout.library_row, null);
|
||||
break;
|
||||
case 2:
|
||||
adapter = mSongAdapter = new MediaAdapter(activity, MediaUtils.TYPE_SONG, false, true, mPendingSongLimiter);
|
||||
adapter = mSongAdapter = new MediaAdapter(activity, MediaUtils.TYPE_SONG, mPendingSongLimiter);
|
||||
mPendingSongLimiter = null;
|
||||
mSongAdapter.setHeaderText(mHeaderText);
|
||||
mSongHeader = header = (TextView)inflater.inflate(R.layout.library_row, null);
|
||||
break;
|
||||
case 3:
|
||||
adapter = mPlaylistAdapter = new MediaAdapter(activity, MediaUtils.TYPE_PLAYLIST, true, false, null);
|
||||
adapter = mPlaylistAdapter = new MediaAdapter(activity, MediaUtils.TYPE_PLAYLIST, null);
|
||||
break;
|
||||
case 4:
|
||||
adapter = new MediaAdapter(activity, MediaUtils.TYPE_GENRE, true, false, null);
|
||||
adapter = new MediaAdapter(activity, MediaUtils.TYPE_GENRE, null);
|
||||
break;
|
||||
case 5:
|
||||
adapter = mFilesAdapter = new FileSystemAdapter(activity, mPendingFileLimiter);
|
||||
@@ -194,15 +206,14 @@ public class LibraryPagerAdapter extends PagerAdapter implements Handler.Callbac
|
||||
throw new IllegalArgumentException("Invalid position: " + position);
|
||||
}
|
||||
|
||||
view = new ListView(activity);
|
||||
view.setOnItemClickListener(activity);
|
||||
view.setOnCreateContextMenuListener(activity);
|
||||
view.setDivider(null);
|
||||
view.setFastScrollEnabled(true);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
||||
CompatHoneycomb.setFastScrollAlwaysVisible(view, true);
|
||||
view = (ListView)inflater.inflate(R.layout.listview, null);
|
||||
view.setOnCreateContextMenuListener(this);
|
||||
view.setOnItemClickListener(this);
|
||||
if (header != null) {
|
||||
header.setText(mHeaderText);
|
||||
header.setTag(position + 1);
|
||||
view.addHeaderView(header);
|
||||
}
|
||||
view.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_INSET);
|
||||
|
||||
view.setAdapter(adapter);
|
||||
if (position != 5)
|
||||
@@ -294,12 +305,12 @@ public class LibraryPagerAdapter extends PagerAdapter implements Handler.Callbac
|
||||
*/
|
||||
public void setHeaderText(String text)
|
||||
{
|
||||
if (mArtistAdapter != null)
|
||||
mArtistAdapter.setHeaderText(text);
|
||||
if (mAlbumAdapter != null)
|
||||
mAlbumAdapter.setHeaderText(text);
|
||||
if (mSongAdapter != null)
|
||||
mSongAdapter.setHeaderText(text);
|
||||
if (mArtistHeader != null)
|
||||
mArtistHeader.setText(text);
|
||||
if (mAlbumHeader != null)
|
||||
mAlbumHeader.setText(text);
|
||||
if (mSongHeader != null)
|
||||
mSongHeader.setText(text);
|
||||
mHeaderText = text;
|
||||
}
|
||||
|
||||
@@ -632,4 +643,32 @@ public class LibraryPagerAdapter extends PagerAdapter implements Handler.Callbac
|
||||
// So we use both.
|
||||
setPrimaryItem(null, position, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the row data used by LibraryActivity.
|
||||
*/
|
||||
private Intent createHeaderIntent(View header)
|
||||
{
|
||||
int type = (Integer)header.getTag();
|
||||
Intent intent = new Intent();
|
||||
intent.putExtra(LibraryAdapter.DATA_ID, LibraryAdapter.HEADER_ID);
|
||||
intent.putExtra(LibraryAdapter.DATA_TYPE, type);
|
||||
return intent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo)
|
||||
{
|
||||
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
|
||||
View targetView = info.targetView;
|
||||
Intent intent = info.id == -1 ? createHeaderIntent(targetView) : mCurrentAdapter.createData(targetView);
|
||||
mActivity.onCreateContextMenu(menu, intent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> list, View view, int position, long id)
|
||||
{
|
||||
Intent intent = id == -1 ? createHeaderIntent(view) : mCurrentAdapter.createData(view);
|
||||
mActivity.onItemClicked(intent);
|
||||
}
|
||||
}
|
||||
@@ -23,15 +23,23 @@
|
||||
package org.kreed.vanilla;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
import android.database.DatabaseUtils;
|
||||
import android.graphics.Color;
|
||||
import android.net.Uri;
|
||||
import android.provider.BaseColumns;
|
||||
import android.provider.MediaStore;
|
||||
import android.text.Spannable;
|
||||
import android.text.SpannableStringBuilder;
|
||||
import android.text.style.ForegroundColorSpan;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.CursorAdapter;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.SectionIndexer;
|
||||
import android.widget.TextView;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
@@ -45,13 +53,22 @@ import java.util.regex.Pattern;
|
||||
* to a specific group to be displayed, e.g. only songs from a certain artist.
|
||||
* See getLimiter and setLimiter for details.
|
||||
*/
|
||||
public class MediaAdapter extends CursorAdapter implements SectionIndexer, LibraryAdapter {
|
||||
public class MediaAdapter
|
||||
extends CursorAdapter
|
||||
implements SectionIndexer
|
||||
, LibraryAdapter
|
||||
, View.OnClickListener
|
||||
{
|
||||
private static final Pattern SPACE_SPLIT = Pattern.compile("\\s+");
|
||||
|
||||
/**
|
||||
* A context to use.
|
||||
*/
|
||||
private final Context mContext;
|
||||
private final LibraryActivity mActivity;
|
||||
/**
|
||||
* A LayoutInflater to use.
|
||||
*/
|
||||
private final LayoutInflater mInflater;
|
||||
/**
|
||||
* The type of media represented by this adapter. Must be one of the
|
||||
* MediaUtils.FIELD_* constants. Determines which content provider to query for
|
||||
@@ -77,10 +94,6 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer, Libra
|
||||
* The columns to query from the content provider.
|
||||
*/
|
||||
private String[] mProjection;
|
||||
/**
|
||||
* If true, show an expand arrow next the the text in each view.
|
||||
*/
|
||||
private final boolean mExpandable;
|
||||
/**
|
||||
* A limiter is used for filtering. The intention is to restrict items
|
||||
* displayed in the list to only those of a specific artist or album, as
|
||||
@@ -99,15 +112,6 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer, Libra
|
||||
* The sections used by the indexer.
|
||||
*/
|
||||
private Object[] mSections;
|
||||
/**
|
||||
* True if this adapter should have a special MediaView with custom text in
|
||||
* the first row.
|
||||
*/
|
||||
private final boolean mHasHeader;
|
||||
/**
|
||||
* The text to show in the header.
|
||||
*/
|
||||
private String mHeaderText;
|
||||
/**
|
||||
* The sort order for use with buildSongQuery().
|
||||
*/
|
||||
@@ -127,30 +131,32 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer, Libra
|
||||
* instead of ascending).
|
||||
*/
|
||||
private int mSortMode;
|
||||
/**
|
||||
* The layout used for each row.
|
||||
*/
|
||||
private int mLayout;
|
||||
|
||||
/**
|
||||
* Construct a MediaAdapter representing the given <code>type</code> of
|
||||
* media.
|
||||
*
|
||||
* @param context A context to use.
|
||||
* @param activity The LibraryActivity that will contain this adapter.
|
||||
* @param type The type of media to represent. Must be one of the
|
||||
* Song.TYPE_* constants. This determines which content provider to query
|
||||
* and what fields to display in the views.
|
||||
* @param expandable Whether an expand arrow should be shown to the right
|
||||
* of the views' text
|
||||
* @param hasHeader Whether this view has a header row.
|
||||
* @param limiter An initial limiter to use
|
||||
*/
|
||||
public MediaAdapter(Context context, int type, boolean expandable, boolean hasHeader, Limiter limiter)
|
||||
public MediaAdapter(LibraryActivity activity, int type, Limiter limiter)
|
||||
{
|
||||
super(context, null, false);
|
||||
super(activity, null, false);
|
||||
|
||||
mContext = context;
|
||||
mActivity = activity;
|
||||
mType = type;
|
||||
mExpandable = expandable;
|
||||
mHasHeader = hasHeader;
|
||||
mLimiter = limiter;
|
||||
mIndexer = new MusicAlphabetIndexer(1);
|
||||
mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
|
||||
switch (type) {
|
||||
case MediaUtils.TYPE_ARTIST:
|
||||
@@ -160,6 +166,7 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer, Libra
|
||||
mSongSort = MediaUtils.DEFAULT_SORT;
|
||||
mSortEntries = new int[] { R.string.name, R.string.number_of_tracks };
|
||||
mSortValues = new String[] { "artist_key %1$s", "number_of_tracks %1$s,artist_key %1$s" };
|
||||
mLayout = R.layout.library_row_expandable;
|
||||
break;
|
||||
case MediaUtils.TYPE_ALBUM:
|
||||
mStore = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
|
||||
@@ -169,6 +176,7 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer, Libra
|
||||
mSongSort = "album_key,track";
|
||||
mSortEntries = new int[] { R.string.name, R.string.artist_album, R.string.year, R.string.number_of_tracks };
|
||||
mSortValues = new String[] { "album_key %1$s", "artist_key %1$s,album_key %1$s", "minyear %1$s,album_key %1$s", "numsongs %1$s,album_key %1$s" };
|
||||
mLayout = R.layout.library_row_expandable;
|
||||
break;
|
||||
case MediaUtils.TYPE_SONG:
|
||||
mStore = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
|
||||
@@ -176,6 +184,7 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer, Libra
|
||||
mFieldKeys = new String[] { MediaStore.Audio.Media.ARTIST_KEY, MediaStore.Audio.Media.ALBUM_KEY, MediaStore.Audio.Media.TITLE_KEY };
|
||||
mSortEntries = new int[] { R.string.name, R.string.artist_album_track, R.string.artist_album_title, R.string.artist_year, R.string.year };
|
||||
mSortValues = new String[] { "title_key %1$s", "artist_key %1$s,album_key %1$s,track %1$s", "artist_key %1$s,album_key %1$s,title_key %1$s", "artist_key %1$s,year %1$s,track %1$s", "year %1$s,title_key %1$s" };
|
||||
mLayout = R.layout.library_row;
|
||||
break;
|
||||
case MediaUtils.TYPE_PLAYLIST:
|
||||
mStore = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
|
||||
@@ -183,6 +192,7 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer, Libra
|
||||
mFieldKeys = null;
|
||||
mSortEntries = new int[] { R.string.name, R.string.date_added };
|
||||
mSortValues = new String[] { "name %1$s", "date_added %1$s" };
|
||||
mLayout = R.layout.library_row_expandable;
|
||||
break;
|
||||
case MediaUtils.TYPE_GENRE:
|
||||
mStore = MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI;
|
||||
@@ -190,6 +200,7 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer, Libra
|
||||
mFieldKeys = null;
|
||||
mSortEntries = new int[] { R.string.name };
|
||||
mSortValues = new String[] { "name %1$s" };
|
||||
mLayout = R.layout.library_row_expandable;
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Invalid value for type: " + type);
|
||||
@@ -201,75 +212,6 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer, Libra
|
||||
mProjection = new String[] { BaseColumns._ID, mFields[mFields.length - 1], mFields[0] };
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount()
|
||||
{
|
||||
int count = super.getCount();
|
||||
if (count == 0)
|
||||
return 0;
|
||||
else if (mHasHeader)
|
||||
return count + 1;
|
||||
else
|
||||
return count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getItem(int pos)
|
||||
{
|
||||
if (mHasHeader) {
|
||||
if (pos == 0)
|
||||
return null;
|
||||
else
|
||||
pos -= 1;
|
||||
}
|
||||
|
||||
return super.getItem(pos);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int pos)
|
||||
{
|
||||
if (mHasHeader) {
|
||||
if (pos == 0)
|
||||
return -1;
|
||||
else
|
||||
pos -= 1;
|
||||
}
|
||||
|
||||
return super.getItemId(pos);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int pos, View convertView, ViewGroup parent)
|
||||
{
|
||||
if (mHasHeader) {
|
||||
if (pos == 0) {
|
||||
MediaView view;
|
||||
if (convertView == null)
|
||||
view = new MediaView(mContext, null, mExpandable ? MediaView.sExpander : null);
|
||||
else
|
||||
view = (MediaView)convertView;
|
||||
view.makeHeader(mHeaderText);
|
||||
return view;
|
||||
} else {
|
||||
pos -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
return super.getView(pos, convertView, parent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify the header text to be shown in the first row.
|
||||
*
|
||||
* @param text The new text.
|
||||
*/
|
||||
public void setHeaderText(String text)
|
||||
{
|
||||
mHeaderText = text;
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFilter(String filter)
|
||||
{
|
||||
@@ -362,7 +304,7 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer, Libra
|
||||
@Override
|
||||
public Object query()
|
||||
{
|
||||
return buildQuery(mProjection, false).runQuery(mContext.getContentResolver());
|
||||
return buildQuery(mProjection, false).runQuery(mActivity.getContentResolver());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -474,14 +416,7 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer, Libra
|
||||
return 0;
|
||||
if (section == getSections().length)
|
||||
return getCount();
|
||||
|
||||
int offset = 0;
|
||||
if (mHasHeader) {
|
||||
if (section == 0)
|
||||
return 0;
|
||||
offset = 1;
|
||||
}
|
||||
return offset + mIndexer.getPositionForSection(section);
|
||||
return mIndexer.getPositionForSection(section);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -489,23 +424,38 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer, Libra
|
||||
{
|
||||
if (mSortMode != 0)
|
||||
return 0;
|
||||
|
||||
if (mHasHeader) {
|
||||
if (position == 0)
|
||||
return 0;
|
||||
else
|
||||
position -= 1;
|
||||
}
|
||||
return mIndexer.getSectionForPosition(position);
|
||||
}
|
||||
|
||||
private static class ViewHolder {
|
||||
public long id;
|
||||
public String title;
|
||||
public TextView text;
|
||||
public ImageView arrow;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the values in the given view.
|
||||
*/
|
||||
@Override
|
||||
public void bindView(View view, Context context, Cursor cursor)
|
||||
{
|
||||
((MediaView)view).updateMedia(cursor, mFields.length > 1);
|
||||
ViewHolder holder = (ViewHolder)view.getTag();
|
||||
holder.id = cursor.getLong(0);
|
||||
if (mFields.length > 1) {
|
||||
String line1 = cursor.getString(1);
|
||||
String line2 = cursor.getString(2);
|
||||
SpannableStringBuilder sb = new SpannableStringBuilder(line1);
|
||||
sb.append('\n');
|
||||
sb.append(line2);
|
||||
sb.setSpan(new ForegroundColorSpan(Color.GRAY), line1.length() + 1, sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||
holder.text.setText(sb);
|
||||
holder.title = line1;
|
||||
} else {
|
||||
String title = cursor.getString(1);
|
||||
holder.text.setText(title);
|
||||
holder.title = title;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -514,7 +464,22 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer, Libra
|
||||
@Override
|
||||
public View newView(Context context, Cursor cursor, ViewGroup parent)
|
||||
{
|
||||
return new MediaView(mContext, null, mExpandable ? MediaView.sExpander : null);
|
||||
int layout = mLayout;
|
||||
View view = mInflater.inflate(layout, null);
|
||||
ViewHolder holder = new ViewHolder();
|
||||
view.setTag(holder);
|
||||
|
||||
if (layout == R.layout.library_row_expandable) {
|
||||
holder.text = (TextView)view.findViewById(R.id.text);
|
||||
holder.arrow = (ImageView)view.findViewById(R.id.arrow);
|
||||
holder.arrow.setOnClickListener(this);
|
||||
} else {
|
||||
holder.text = (TextView)view;
|
||||
view.setLongClickable(true);
|
||||
}
|
||||
|
||||
holder.text.setOnClickListener(this);
|
||||
return view;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -574,4 +539,30 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer, Libra
|
||||
{
|
||||
return mSortMode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Intent createData(View view)
|
||||
{
|
||||
ViewHolder holder = (ViewHolder)view.getTag();
|
||||
Intent intent = new Intent();
|
||||
intent.putExtra(LibraryAdapter.DATA_TYPE, mType);
|
||||
intent.putExtra(LibraryAdapter.DATA_ID, holder.id);
|
||||
intent.putExtra(LibraryAdapter.DATA_TITLE, holder.title);
|
||||
intent.putExtra(LibraryAdapter.DATA_EXPANDABLE, mType != MediaUtils.TYPE_SONG);
|
||||
return intent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View view)
|
||||
{
|
||||
int id = view.getId();
|
||||
if (mLayout == R.layout.library_row_expandable)
|
||||
view = (View)view.getParent();
|
||||
Intent intent = createData(view);
|
||||
if (id == R.id.arrow) {
|
||||
mActivity.onItemExpanded(intent);
|
||||
} else {
|
||||
mActivity.onItemClicked(intent);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,320 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2010, 2011 Christopher Eby <kreed@kreed.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package org.kreed.vanilla;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.database.Cursor;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.util.TypedValue;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
|
||||
/**
|
||||
* Single view that paints one or two text fields and an optional arrow
|
||||
* to the right side.
|
||||
*/
|
||||
public final class MediaView extends View {
|
||||
/**
|
||||
* Views that have been made into a header by
|
||||
* {@link MediaView#makeHeader(String)} will be given this id.
|
||||
*/
|
||||
public static final long HEADER_ID = -1;
|
||||
|
||||
/**
|
||||
* The expander arrow bitmap used in all views that have expanders.
|
||||
*/
|
||||
public static Bitmap sExpander;
|
||||
/**
|
||||
* The paint object, cached for reuse.
|
||||
*/
|
||||
private static Paint sPaint;
|
||||
/**
|
||||
* The text size used for the text in all views.
|
||||
*/
|
||||
private static int sTextSize;
|
||||
|
||||
public static void init(Context context)
|
||||
{
|
||||
Resources res = context.getResources();
|
||||
sExpander = BitmapFactory.decodeResource(res, R.drawable.arrow);
|
||||
sTextSize = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, res.getDisplayMetrics());
|
||||
|
||||
sPaint = new Paint();
|
||||
sPaint.setTextSize(sTextSize);
|
||||
sPaint.setAntiAlias(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* The cached measured view height.
|
||||
*/
|
||||
private final int mViewHeight;
|
||||
/**
|
||||
* An optional bitmap to display on the left of the view.
|
||||
*/
|
||||
private final Bitmap mLeftBitmap;
|
||||
/**
|
||||
* An optional bitmap to display on the right of the view.
|
||||
*/
|
||||
private final Bitmap mRightBitmap;
|
||||
|
||||
/**
|
||||
* The MediaStore id of the media represented by this view.
|
||||
*/
|
||||
private long mId;
|
||||
/**
|
||||
* The primary text field in the view, displayed on the upper line.
|
||||
*/
|
||||
private String mTitle;
|
||||
/**
|
||||
* The secondary text field in the view, displayed on the lower line.
|
||||
*/
|
||||
private String mSubTitle;
|
||||
/**
|
||||
* True to show the bitmaps, false to hide them. Defaults to true.
|
||||
*/
|
||||
private boolean mShowBitmaps = true;
|
||||
|
||||
private boolean mBottomGravity;
|
||||
/**
|
||||
* The x coordinate of the last touch event.
|
||||
*/
|
||||
private int mTouchX;
|
||||
|
||||
/**
|
||||
* Construct a MediaView.
|
||||
*
|
||||
* @param context A Context to use.
|
||||
* @param leftBitmap An optional bitmap to be shown in the left side of
|
||||
* the view.
|
||||
* @param rightBitmap An optional bitmap to be shown in the right side of
|
||||
* the view.
|
||||
*/
|
||||
public MediaView(Context context, Bitmap leftBitmap, Bitmap rightBitmap)
|
||||
{
|
||||
super(context);
|
||||
mLeftBitmap = leftBitmap;
|
||||
mRightBitmap = rightBitmap;
|
||||
|
||||
int height = 7 * sTextSize / 2;
|
||||
if (mLeftBitmap != null)
|
||||
height = Math.max(height, mLeftBitmap.getHeight() + sTextSize);
|
||||
if (mRightBitmap != null)
|
||||
height = Math.max(height, mRightBitmap.getHeight() + sTextSize);
|
||||
mViewHeight = height;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether to show the left and right bitmaps. By default, will show them.
|
||||
*
|
||||
* @param show If false, do not show the bitmaps.
|
||||
*/
|
||||
public void setShowBitmaps(boolean show)
|
||||
{
|
||||
mShowBitmaps = show;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request the cached height and maximum width from the layout.
|
||||
*/
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
|
||||
{
|
||||
if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY)
|
||||
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
|
||||
else
|
||||
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), mViewHeight);
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw the view on the given canvas.
|
||||
*/
|
||||
@Override
|
||||
public void onDraw(Canvas canvas)
|
||||
{
|
||||
if (mTitle == null || getHeight() == 1)
|
||||
return;
|
||||
|
||||
int width = getWidth();
|
||||
int height = mViewHeight;
|
||||
int padding = sTextSize / 2;
|
||||
int xOffset = 0;
|
||||
|
||||
if (mBottomGravity)
|
||||
canvas.translate(0, getHeight() - mViewHeight);
|
||||
|
||||
Paint paint = sPaint;
|
||||
|
||||
if (mShowBitmaps && mRightBitmap != null) {
|
||||
Bitmap expander = mRightBitmap;
|
||||
width -= padding * 4 + expander.getWidth();
|
||||
|
||||
paint.setColor(Color.GRAY);
|
||||
canvas.drawLine(width, padding, width, height - padding, paint);
|
||||
paint.setPathEffect(null);
|
||||
|
||||
canvas.drawBitmap(expander, width + padding * 2, (height - expander.getHeight()) / 2, paint);
|
||||
}
|
||||
|
||||
if (mShowBitmaps && mLeftBitmap != null) {
|
||||
Bitmap expander = mLeftBitmap;
|
||||
canvas.drawBitmap(expander, padding, (height - expander.getHeight()) / 2, paint);
|
||||
xOffset = expander.getWidth() + padding;
|
||||
}
|
||||
|
||||
canvas.save();
|
||||
canvas.clipRect(padding, 0, width - padding, height);
|
||||
|
||||
int allocatedHeight;
|
||||
|
||||
if (mSubTitle != null) {
|
||||
allocatedHeight = height / 2 - padding * 3 / 2;
|
||||
|
||||
paint.setColor(Color.GRAY);
|
||||
canvas.drawText(mSubTitle, xOffset + padding, height / 2 + padding / 2 + (allocatedHeight - sTextSize) / 2 - paint.ascent(), paint);
|
||||
} else {
|
||||
allocatedHeight = height - padding * 2;
|
||||
}
|
||||
|
||||
paint.setColor(Color.WHITE);
|
||||
canvas.drawText(mTitle, xOffset + padding, padding + (allocatedHeight - sTextSize) / 2 - paint.ascent(), paint);
|
||||
canvas.restore();
|
||||
|
||||
width = getWidth();
|
||||
paint.setColor(Color.GRAY);
|
||||
canvas.drawLine(0, height - 1, width, height - 1, paint);
|
||||
paint.setShader(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the gravity of the view (top or bottom), determing which edge to
|
||||
* align the content to.
|
||||
*
|
||||
* @param bottom True for bottom gravity; false for top gravity.
|
||||
*/
|
||||
public void setBottomGravity(boolean bottom)
|
||||
{
|
||||
mBottomGravity = bottom;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the desired height for this view.
|
||||
*
|
||||
* @return The measured view height.
|
||||
*/
|
||||
public int getPreferredHeight()
|
||||
{
|
||||
return mViewHeight;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the MediaStore id of the media represented by this view.
|
||||
*/
|
||||
public long getMediaId()
|
||||
{
|
||||
return mId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the title of this view, the primary/upper field.
|
||||
*/
|
||||
public String getTitle()
|
||||
{
|
||||
return mTitle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the view has a right bitmap that is visible.
|
||||
*/
|
||||
public boolean hasRightBitmap()
|
||||
{
|
||||
return mRightBitmap != null && mShowBitmaps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the right bitmap was pressed in the last touch event.
|
||||
*/
|
||||
public boolean isRightBitmapPressed()
|
||||
{
|
||||
return mRightBitmap != null && mShowBitmaps && mTouchX > getWidth() - mRightBitmap.getWidth() - 2 * sTextSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set this view to be a header (custom text, never expandable).
|
||||
*
|
||||
* @param text The text to show.
|
||||
*/
|
||||
public void makeHeader(String text)
|
||||
{
|
||||
mShowBitmaps = false;
|
||||
mId = HEADER_ID;
|
||||
mTitle = text;
|
||||
mSubTitle = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the fields in this view with the data from the given Cursor.
|
||||
*
|
||||
* @param cursor A cursor moved to the correct position. The first
|
||||
* column must be the id of the media, the second the primary field.
|
||||
* If this adapter contains more than one field, the third column
|
||||
* must contain the secondary field.
|
||||
* @param useSecondary True if the secondary field should be read.
|
||||
*/
|
||||
public void updateMedia(Cursor cursor, boolean useSecondary)
|
||||
{
|
||||
mShowBitmaps = true;
|
||||
mId = cursor.getLong(0);
|
||||
mTitle = cursor.getString(1);
|
||||
if (useSecondary)
|
||||
mSubTitle = cursor.getString(2);
|
||||
invalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the id and title in this view.
|
||||
*
|
||||
* @param id The new id.
|
||||
* @param title The new title for the view.
|
||||
*/
|
||||
public void setData(long id, String title)
|
||||
{
|
||||
mId = id;
|
||||
mTitle = title;
|
||||
invalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update mExpanderPressed.
|
||||
*/
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent event)
|
||||
{
|
||||
mTouchX = (int)event.getX();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -95,13 +95,9 @@ public class PlaylistActivity extends Activity
|
||||
HandlerThread thread = new HandlerThread(getClass().getName());
|
||||
thread.start();
|
||||
|
||||
MediaView.init(this);
|
||||
|
||||
setContentView(R.layout.playlist_activity);
|
||||
|
||||
DragListView view = (DragListView)findViewById(R.id.playlist);
|
||||
view.setDivider(null);
|
||||
view.setFastScrollEnabled(true);
|
||||
view.setOnItemClickListener(this);
|
||||
view.setOnCreateContextMenuListener(this);
|
||||
mListView = view;
|
||||
@@ -111,7 +107,7 @@ public class PlaylistActivity extends Activity
|
||||
mEditButton.setOnClickListener(this);
|
||||
mDeleteButton = (Button)header.findViewById(R.id.delete);
|
||||
mDeleteButton.setOnClickListener(this);
|
||||
view.addHeaderView(header);
|
||||
view.addHeaderView(header, null, false);
|
||||
|
||||
mLooper = thread.getLooper();
|
||||
mAdapter = new PlaylistAdapter(this, mLooper);
|
||||
@@ -252,13 +248,8 @@ public class PlaylistActivity extends Activity
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
|
||||
{
|
||||
if (view instanceof MediaView) {
|
||||
MediaView mediaView = (MediaView)view;
|
||||
if (mediaView.isRightBitmapPressed()) {
|
||||
mAdapter.remove(id);
|
||||
} else if (!mEditing && mDefaultAction != LibraryActivity.ACTION_DO_NOTHING) {
|
||||
performAction(mDefaultAction, position, (Long)mediaView.getTag());
|
||||
}
|
||||
if (!mEditing && mDefaultAction != LibraryActivity.ACTION_DO_NOTHING) {
|
||||
performAction(mDefaultAction, position, (Long)view.getTag());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,18 +26,18 @@ import android.content.ContentResolver;
|
||||
import android.content.ContentUris;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.database.Cursor;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.Message;
|
||||
import android.provider.MediaStore;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.CursorAdapter;
|
||||
import android.widget.TextView;
|
||||
|
||||
/**
|
||||
* CursorAdapter backed by MediaStore playlists.
|
||||
@@ -54,12 +54,12 @@ public class PlaylistAdapter extends CursorAdapter implements Handler.Callback {
|
||||
private final Context mContext;
|
||||
private final Handler mWorkerHandler;
|
||||
private final Handler mUiHandler;
|
||||
private final LayoutInflater mInflater;
|
||||
private final Drawable mExpander;
|
||||
|
||||
private long mPlaylistId;
|
||||
|
||||
private boolean mEditable;
|
||||
private final Bitmap mDragBitmap;
|
||||
private final Bitmap mDeleteBitmap;
|
||||
|
||||
/**
|
||||
* Create a playlist adapter.
|
||||
@@ -74,10 +74,8 @@ public class PlaylistAdapter extends CursorAdapter implements Handler.Callback {
|
||||
mContext = context;
|
||||
mUiHandler = new Handler(this);
|
||||
mWorkerHandler = new Handler(worker, this);
|
||||
|
||||
Resources res = context.getResources();
|
||||
mDragBitmap = BitmapFactory.decodeResource(res, R.drawable.grabber);
|
||||
mDeleteBitmap = BitmapFactory.decodeResource(res, R.drawable.close);
|
||||
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
mExpander = context.getResources().getDrawable(R.drawable.grabber);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,10 +107,10 @@ public class PlaylistAdapter extends CursorAdapter implements Handler.Callback {
|
||||
@Override
|
||||
public void bindView(View view, Context context, Cursor cursor)
|
||||
{
|
||||
MediaView mediaView = (MediaView)view;
|
||||
mediaView.updateMedia(cursor, true);
|
||||
mediaView.setTag(cursor.getLong(3));
|
||||
mediaView.setShowBitmaps(mEditable);
|
||||
TextView textView = (TextView)view;
|
||||
textView.setText(cursor.getString(1));
|
||||
textView.setCompoundDrawablesWithIntrinsicBounds(mEditable ? mExpander : null, null, null, null);
|
||||
textView.setTag(cursor.getLong(3));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -121,7 +119,7 @@ public class PlaylistAdapter extends CursorAdapter implements Handler.Callback {
|
||||
@Override
|
||||
public View newView(Context context, Cursor cursor, ViewGroup parent)
|
||||
{
|
||||
return new MediaView(context, mDragBitmap, mDeleteBitmap);
|
||||
return mInflater.inflate(R.layout.playlist_row, null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user