Add new filechooser

This commit is contained in:
Sebastian Dröge 2012-10-02 16:54:27 +02:00
parent e3b897f4b5
commit e67b567904
11 changed files with 505 additions and 16 deletions
gst-sdk/tutorials/android-tutorial-1

View file

@ -7,7 +7,6 @@
<uses-permission android:name="android.permission.INTERNET"/>
<uses-feature android:glEsVersion="0x00020000"/>
<application android:label="@string/app_name"
android:debuggable="true"
android:icon="@drawable/gst_sdk_icon">
<activity android:name=".Tutorial1"
android:label="@string/app_name">
@ -16,5 +15,8 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.lamerman.FileDialog"
android:label="@string/filechooser_name">
</activity>
</application>
</manifest>

View file

@ -267,6 +267,13 @@ static void *app_function (void *userdata) {
data->pipeline = gst_element_factory_make ("playbin2", NULL);
if (0) {
GstElement *fakesink;
fakesink = gst_element_factory_make ("fakesink", NULL);
g_object_set (fakesink, "sync", TRUE, NULL);
g_object_set (data->pipeline, "video-sink", fakesink, NULL);
}
if (data->native_window) {
GST_DEBUG ("Native window already received, notifying the pipeline about it.");
gst_x_overlay_set_window_handle (GST_X_OVERLAY (data->pipeline), (guintptr)data->native_window);
@ -342,12 +349,12 @@ void gst_native_finalize (JNIEnv* env, jobject thiz) {
void gst_native_set_uri (JNIEnv* env, jobject thiz, jstring uri) {
CustomData *data = GET_CUSTOM_DATA (env, thiz, custom_data_field_id);
if (!data) return;
if (!data || !data->pipeline) return;
const jbyte *char_uri = (*env)->GetStringUTFChars (env, uri, NULL);
GST_DEBUG ("Setting URI to %s", char_uri);
if (data->target_state >= GST_STATE_READY)
gst_element_set_state (data->pipeline, GST_STATE_READY);
g_object_set(data->pipeline, "uri", char_uri);
g_object_set(data->pipeline, "uri", char_uri, NULL);
(*env)->ReleaseStringUTFChars (env, uri, char_uri);
data->is_live = (gst_element_set_state (data->pipeline, data->target_state) == GST_STATE_CHANGE_NO_PREROLL);
}
@ -428,12 +435,14 @@ void gst_native_surface_finalize (JNIEnv *env, jobject thiz) {
return;
}
GST_DEBUG ("Releasing Native Window %p", data->native_window);
ANativeWindow_release (data->native_window);
data->native_window = NULL;
if (data->pipeline) {
gst_x_overlay_set_window_handle (GST_X_OVERLAY (data->pipeline), (guintptr)NULL);
gst_x_overlay_set_window_handle (GST_X_OVERLAY (data->pipeline), (guintptr)NULL);
gst_element_set_state (data->pipeline, GST_STATE_NULL);
}
ANativeWindow_release (data->native_window);
data->native_window = NULL;
}
static JNINativeMethod native_methods[] = {

Binary file not shown.

After

(image error) Size: 571 B

Binary file not shown.

After

(image error) Size: 392 B

Binary file not shown.

After

(image error) Size: 2.5 KiB

View file

@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/relativeLayout01"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="@+id/fdLinearLayoutList"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_alignParentBottom="true">
<LinearLayout android:id="@+id/fdLinearLayoutSelect"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" android:paddingLeft="10dp"
android:paddingRight="10dp" android:paddingBottom="5dp">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<Button android:id="@+id/fdButtonNew" android:layout_height="wrap_content"
android:layout_width="0dip" android:layout_weight=".3"
android:text="@string/nnew"></Button>
<Button android:id="@+id/fdButtonSelect" android:layout_height="wrap_content"
android:layout_width="0dip" android:layout_weight=".7"
android:text="@string/select"></Button>
</LinearLayout>
</LinearLayout>
<LinearLayout android:id="@+id/fdLinearLayoutCreate"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" android:paddingLeft="10dp"
android:paddingRight="10dp" android:paddingBottom="5dp">
<TextView android:id="@+id/textViewFilename" android:text="@string/file_name"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<EditText android:text="" android:id="@+id/fdEditTextFile"
android:layout_width="fill_parent" android:layout_height="wrap_content"></EditText>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<Button android:id="@+id/fdButtonCancel" android:layout_height="wrap_content"
android:layout_width="0dip" android:layout_weight=".3"
android:text="@string/cancel"></Button>
<Button android:id="@+id/fdButtonCreate" android:layout_height="wrap_content"
android:layout_width="0dip" android:layout_weight=".7"
android:text="@string/create"></Button>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_above="@+id/fdLinearLayoutList">
<TextView android:id="@+id/path" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView android:id="@android:id/list" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView android:id="@android:id/empty"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:text="@string/no_data" />
</LinearLayout>
</RelativeLayout>

View file

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView android:layout_alignParentLeft="true" android:id="@+id/fdrowimage"
android:layout_height="35dp" android:layout_width="wrap_content"
android:paddingRight="5dp" android:paddingLeft="3dp"></ImageView>
<TextView android:text="@+id/fdrowtext" android:layout_width="wrap_content"
android:id="@+id/fdrowtext" android:layout_toRightOf="@+id/fdrowimage"
android:layout_alignTop="@+id/fdrowimage" android:layout_alignBottom="@+id/fdrowimage"
android:gravity="center_vertical" android:layout_height="35dp"
android:textSize="23dp"></TextView>
</RelativeLayout>

View file

@ -4,4 +4,15 @@
<string name="button_play">Play</string>
<string name="button_stop">Stop</string>
<string name="button_select">Select</string>
<string name="filechooser_name">Android tutorial 1</string>
<string name="location">Location</string>
<string name="cant_read_folder">folder can\'t be read!</string>
<string name="nnew">New</string>
<string name="select">Select</string>
<string name="file_name">File name:</string>
<string name="cancel">Cancel</string>
<string name="create">Save</string>
<string name="no_data">No Data</string>
<color name="gray">#ffff0000</color>
<string name="err">Error</string>
</resources>

View file

@ -20,15 +20,14 @@ import java.util.Date;
import java.util.TimeZone;
import com.gst_sdk.GStreamer;
import com.lamerman.FileDialog;
import com.lamerman.SelectionMode;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
@ -97,8 +96,9 @@ public class Tutorial1 extends Activity implements SurfaceHolder.Callback, OnSee
ImageButton select = (ImageButton) this.findViewById(R.id.button_select);
select.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK);
i.setDataAndType(Uri.fromFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES)), "video/*;audio/*");
Intent i = new Intent(getBaseContext(), FileDialog.class);
i.putExtra(FileDialog.START_PATH, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath());
i.putExtra(FileDialog.SELECTION_MODE, SelectionMode.MODE_OPEN);
startActivityForResult(i, PICK_FILE_CODE);
}
});
@ -268,11 +268,7 @@ public class Tutorial1 extends Activity implements SurfaceHolder.Callback, OnSee
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK && requestCode == PICK_FILE_CODE) {
String[] proj = { MediaStore.Video.Media.DATA };
Cursor c = managedQuery(data.getData(), proj, null, null, null);
int column_index = c.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
c.moveToFirst();
mediaUri = "file://" + c.getString(column_index);
mediaUri = "file://" + data.getStringExtra(FileDialog.RESULT_PATH);
setMediaUri();
}
}

View file

@ -0,0 +1,383 @@
package com.lamerman;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.TreeMap;
import com.gst_sdk_tutorials.tutorial_1.R;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
/**
* Activity para escolha de arquivos/diretorios.
*
* @author android
*
*/
public class FileDialog extends ListActivity {
/**
* Chave de um item da lista de paths.
*/
private static final String ITEM_KEY = "key";
/**
* Imagem de um item da lista de paths (diretorio ou arquivo).
*/
private static final String ITEM_IMAGE = "image";
/**
* Diretorio raiz.
*/
private static final String ROOT = "/";
/**
* Parametro de entrada da Activity: path inicial. Padrao: ROOT.
*/
public static final String START_PATH = "START_PATH";
/**
* Parametro de entrada da Activity: filtro de formatos de arquivos. Padrao:
* null.
*/
public static final String FORMAT_FILTER = "FORMAT_FILTER";
/**
* Parametro de saida da Activity: path escolhido. Padrao: null.
*/
public static final String RESULT_PATH = "RESULT_PATH";
/**
* Parametro de entrada da Activity: tipo de selecao: pode criar novos paths
* ou nao. Padrao: nao permite.
*
* @see {@link SelectionMode}
*/
public static final String SELECTION_MODE = "SELECTION_MODE";
/**
* Parametro de entrada da Activity: se e permitido escolher diretorios.
* Padrao: falso.
*/
public static final String CAN_SELECT_DIR = "CAN_SELECT_DIR";
private List<String> path = null;
private TextView myPath;
private EditText mFileName;
private ArrayList<HashMap<String, Object>> mList;
private Button selectButton;
private LinearLayout layoutSelect;
private LinearLayout layoutCreate;
private InputMethodManager inputManager;
private String parentPath;
private String currentPath = ROOT;
private int selectionMode = SelectionMode.MODE_CREATE;
private String[] formatFilter = null;
private boolean canSelectDir = false;
private File selectedFile;
private HashMap<String, Integer> lastPositions = new HashMap<String, Integer>();
/**
* Called when the activity is first created. Configura todos os parametros
* de entrada e das VIEWS..
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setResult(RESULT_CANCELED, getIntent());
setContentView(R.layout.file_dialog_main);
myPath = (TextView) findViewById(R.id.path);
mFileName = (EditText) findViewById(R.id.fdEditTextFile);
inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
selectButton = (Button) findViewById(R.id.fdButtonSelect);
selectButton.setEnabled(false);
selectButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (selectedFile != null) {
getIntent().putExtra(RESULT_PATH, selectedFile.getPath());
setResult(RESULT_OK, getIntent());
finish();
}
}
});
final Button newButton = (Button) findViewById(R.id.fdButtonNew);
newButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setCreateVisible(v);
mFileName.setText("");
mFileName.requestFocus();
}
});
selectionMode = getIntent().getIntExtra(SELECTION_MODE, SelectionMode.MODE_CREATE);
formatFilter = getIntent().getStringArrayExtra(FORMAT_FILTER);
canSelectDir = getIntent().getBooleanExtra(CAN_SELECT_DIR, false);
if (selectionMode == SelectionMode.MODE_OPEN) {
newButton.setEnabled(false);
}
layoutSelect = (LinearLayout) findViewById(R.id.fdLinearLayoutSelect);
layoutCreate = (LinearLayout) findViewById(R.id.fdLinearLayoutCreate);
layoutCreate.setVisibility(View.GONE);
final Button cancelButton = (Button) findViewById(R.id.fdButtonCancel);
cancelButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setSelectVisible(v);
}
});
final Button createButton = (Button) findViewById(R.id.fdButtonCreate);
createButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (mFileName.getText().length() > 0) {
getIntent().putExtra(RESULT_PATH, currentPath + "/" + mFileName.getText());
setResult(RESULT_OK, getIntent());
finish();
}
}
});
String startPath = getIntent().getStringExtra(START_PATH);
startPath = startPath != null ? startPath : ROOT;
if (canSelectDir) {
File file = new File(startPath);
selectedFile = file;
selectButton.setEnabled(true);
}
getDir(startPath);
}
private void getDir(String dirPath) {
boolean useAutoSelection = dirPath.length() < currentPath.length();
Integer position = lastPositions.get(parentPath);
getDirImpl(dirPath);
if (position != null && useAutoSelection) {
getListView().setSelection(position);
}
}
/**
* Monta a estrutura de arquivos e diretorios filhos do diretorio fornecido.
*
* @param dirPath
* Diretorio pai.
*/
private void getDirImpl(final String dirPath) {
currentPath = dirPath;
final List<String> item = new ArrayList<String>();
path = new ArrayList<String>();
mList = new ArrayList<HashMap<String, Object>>();
File f = new File(currentPath);
File[] files = f.listFiles();
if (files == null) {
currentPath = ROOT;
f = new File(currentPath);
files = f.listFiles();
}
myPath.setText(getText(R.string.location) + ": " + currentPath);
if (!currentPath.equals(ROOT)) {
item.add(ROOT);
addItem(ROOT, R.drawable.folder);
path.add(ROOT);
item.add("../");
addItem("../", R.drawable.folder);
path.add(f.getParent());
parentPath = f.getParent();
}
TreeMap<String, String> dirsMap = new TreeMap<String, String>();
TreeMap<String, String> dirsPathMap = new TreeMap<String, String>();
TreeMap<String, String> filesMap = new TreeMap<String, String>();
TreeMap<String, String> filesPathMap = new TreeMap<String, String>();
for (File file : files) {
if (file.isDirectory()) {
String dirName = file.getName();
dirsMap.put(dirName, dirName);
dirsPathMap.put(dirName, file.getPath());
} else {
final String fileName = file.getName();
final String fileNameLwr = fileName.toLowerCase();
// se ha um filtro de formatos, utiliza-o
if (formatFilter != null) {
boolean contains = false;
for (int i = 0; i < formatFilter.length; i++) {
final String formatLwr = formatFilter[i].toLowerCase();
if (fileNameLwr.endsWith(formatLwr)) {
contains = true;
break;
}
}
if (contains) {
filesMap.put(fileName, fileName);
filesPathMap.put(fileName, file.getPath());
}
// senao, adiciona todos os arquivos
} else {
filesMap.put(fileName, fileName);
filesPathMap.put(fileName, file.getPath());
}
}
}
item.addAll(dirsMap.tailMap("").values());
item.addAll(filesMap.tailMap("").values());
path.addAll(dirsPathMap.tailMap("").values());
path.addAll(filesPathMap.tailMap("").values());
SimpleAdapter fileList = new SimpleAdapter(this, mList, R.layout.file_dialog_row, new String[] {
ITEM_KEY, ITEM_IMAGE }, new int[] { R.id.fdrowtext, R.id.fdrowimage });
for (String dir : dirsMap.tailMap("").values()) {
addItem(dir, R.drawable.folder);
}
for (String file : filesMap.tailMap("").values()) {
addItem(file, R.drawable.file);
}
fileList.notifyDataSetChanged();
setListAdapter(fileList);
}
private void addItem(String fileName, int imageId) {
HashMap<String, Object> item = new HashMap<String, Object>();
item.put(ITEM_KEY, fileName);
item.put(ITEM_IMAGE, imageId);
mList.add(item);
}
/**
* Quando clica no item da lista, deve-se: 1) Se for diretorio, abre seus
* arquivos filhos; 2) Se puder escolher diretorio, define-o como sendo o
* path escolhido. 3) Se for arquivo, define-o como path escolhido. 4) Ativa
* botao de selecao.
*/
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
File file = new File(path.get(position));
setSelectVisible(v);
if (file.isDirectory()) {
selectButton.setEnabled(false);
if (file.canRead()) {
lastPositions.put(currentPath, position);
getDir(path.get(position));
if (canSelectDir) {
selectedFile = file;
v.setSelected(true);
selectButton.setEnabled(true);
}
} else {
new AlertDialog.Builder(this).setIcon(R.drawable.icon)
.setTitle("[" + file.getName() + "] " + getText(R.string.cant_read_folder))
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
} else {
selectedFile = file;
v.setSelected(true);
selectButton.setEnabled(true);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
selectButton.setEnabled(false);
if (layoutCreate.getVisibility() == View.VISIBLE) {
layoutCreate.setVisibility(View.GONE);
layoutSelect.setVisibility(View.VISIBLE);
} else {
if (!currentPath.equals(ROOT)) {
getDir(parentPath);
} else {
return super.onKeyDown(keyCode, event);
}
}
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
/**
* Define se o botao de CREATE e visivel.
*
* @param v
*/
private void setCreateVisible(View v) {
layoutCreate.setVisibility(View.VISIBLE);
layoutSelect.setVisibility(View.GONE);
inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
selectButton.setEnabled(false);
}
/**
* Define se o botao de SELECT e visivel.
*
* @param v
*/
private void setSelectVisible(View v) {
layoutCreate.setVisibility(View.GONE);
layoutSelect.setVisibility(View.VISIBLE);
inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
selectButton.setEnabled(false);
}
}

View file

@ -0,0 +1,7 @@
package com.lamerman;
public class SelectionMode {
public static final int MODE_CREATE = 0;
public static final int MODE_OPEN = 1;
}