import also for scan

This commit is contained in:
ligi 2013-02-07 23:58:17 +01:00
parent 3fdbed6b45
commit 200a2fe739
3 changed files with 301 additions and 203 deletions

View file

@ -9,6 +9,7 @@ import com.actionbarsherlock.view.Menu;
import java.io.File;
import java.io.InputStream;
import java.util.concurrent.Callable;
public class TicketImportActivity extends SherlockActivity {
@ -33,7 +34,12 @@ public class TicketImportActivity extends SherlockActivity {
i.putExtra("path", path);
(new File(path)).mkdirs();
UnzipPasscodeDialog.show(result, path, ticketImportActivity, i);
UnzipPasscodeDialog.show(result, path, ticketImportActivity, new Callable<Void>() {
@Override
public Void call() throws Exception {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
});
}
super.onPostExecute(result);
}

View file

@ -1,16 +1,15 @@
package org.ligi.ticketviewer;
import java.io.File;
import java.io.FilenameFilter;
import org.json.JSONArray;
import org.json.JSONObject;
import org.ligi.ticketviewer.helper.FileHelper;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
@ -20,140 +19,232 @@ import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.Window;
import org.json.JSONArray;
import org.json.JSONObject;
import org.ligi.ticketviewer.helper.FileHelper;
import java.io.File;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.util.concurrent.Callable;
public class TicketListActivity extends SherlockListActivity {
private String[] passes;
private LayoutInflater inflater;
private String path;
private PassAdapter passadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
path = TicketDefinitions.getPassesDir(this);
File passes_dir=new File(TicketDefinitions.getPassesDir(this));
if (!passes_dir.exists())
passes_dir.mkdirs();
passes =passes_dir.list(new DirFilter());
passadapter=new PassAdapter();
setListAdapter(passadapter);
inflater = getLayoutInflater();
super.onCreate(savedInstanceState);
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
Intent intent = new Intent(TicketListActivity.this, TicketViewActivity.class);
intent.putExtra("path", path + "/" + passes[pos] + "/");
startActivity(intent);
}
});
TextView empty_view=new TextView(this);
empty_view.setText("No passes yet - go get some and come back");
//getListView().setEmptyView(empty_view);
empty_view.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
((ViewGroup)getListView().getParent()).addView(empty_view);
getListView().setEmptyView(empty_view);
}
@Override
protected void onResume() {
super.onResume();
passes = new File(TicketDefinitions.getPassesDir(this)).list(new DirFilter());
passadapter.notifyDataSetChanged();
}
private String[] passes;
private LayoutInflater inflater;
private String path;
private PassAdapter passadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
class DirFilter implements FilenameFilter {
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
@Override
public boolean accept(File dir, String filename) {
return dir.isDirectory();
}
}
refresh_passes_list();
class PassAdapter extends BaseAdapter {
passadapter = new PassAdapter();
setListAdapter(passadapter);
@Override
public int getCount() {
return passes.length;
}
inflater = getLayoutInflater();
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public Object getItem(int position) {
return null;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
Intent intent = new Intent(TicketListActivity.this, TicketViewActivity.class);
intent.putExtra("path", path + "/" + passes[pos] + "/");
startActivity(intent);
}
@Override
public long getItemId(int position) {
return 0;
}
});
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String mPath=path + "/" + passes[position];
View res = inflater.inflate(R.layout.pass_list_item, null);
TextView tv = (TextView) res.findViewById(R.id.label);
TextView more_tv = (TextView) res.findViewById(R.id.descr);
try {
JSONObject pass_json = new JSONObject(FileHelper.file2String(new File(mPath + "/pass.json")));
tv.setText(pass_json.getString("description"));
String more_str = "";
if (pass_json.has("eventTicket")) {
JSONObject eventTicket = pass_json.getJSONObject("eventTicket");
TextView empty_view = new TextView(this);
empty_view.setText("No passes yet - searching for passes");
//getListView().setEmptyView(empty_view);
if (eventTicket.has("primaryFields")) {
JSONArray pri_arr = eventTicket.getJSONArray("primaryFields");
for (int i = 0; i < pri_arr.length(); i++) {
JSONObject sec_obj = pri_arr.getJSONObject(i);
more_str += sec_obj.getString("label") + ":" + sec_obj.getString("value") + "\n";
}
}
if (eventTicket.has("secondaryFields")) {
JSONArray sec_arr = eventTicket.getJSONArray("secondaryFields");
for (int i = 0; i < sec_arr.length(); i++) {
JSONObject sec_obj = sec_arr.getJSONObject(i);
more_str += sec_obj.getString("label") + ":" + sec_obj.getString("value") + "\n";
}
}
}
empty_view.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
more_tv.setText(more_str);
((ViewGroup) getListView().getParent()).addView(empty_view);
getListView().setEmptyView(empty_view);
} catch (Exception e) {
setSupportProgressBarIndeterminateVisibility(true);
}
int size=(int)getResources().getDimension(R.dimen.pass_icon_size);
ImageView icon_img=(ImageView)res.findViewById(R.id.icon);
if (path!=null) {
Bitmap ico=BitmapFactory.decodeFile(mPath+"/logo@2x.png");
if (ico==null)
ico=BitmapFactory.decodeFile(mPath+"/logo.png");
if (ico!=null)
icon_img.setImageBitmap(Bitmap.createScaledBitmap(ico,size,size,false));
}
return res;
}
new ScanForPassesTask().execute();
}
}
private void refresh_passes_list() {
path = TicketDefinitions.getPassesDir(this);
File passes_dir = new File(TicketDefinitions.getPassesDir(this));
if (!passes_dir.exists())
passes_dir.mkdirs();
passes = passes_dir.list(new DirFilter());
}
class ImportAndShowAsyncTask extends ImportAsyncTask {
public ImportAndShowAsyncTask(Activity ticketImportActivity, Uri intent_uri) {
super(ticketImportActivity, intent_uri);
}
@Override
protected void onPostExecute(InputStream result) {
if (result != null) {
String path = TicketDefinitions.getTmpDir(ticketImportActivity);
(new File(path)).mkdirs();
UnzipPasscodeDialog.show(result, path, ticketImportActivity, new Callable<Void>() {
@Override
public Void call() throws Exception {
Log.i("", "refreshing");
runOnUiThread(new Runnable() {
@Override
public void run() {
refresh_passes_list();
passadapter.notifyDataSetChanged();
}
});
return null; //To change body of implemented methods use File | Settings | File Templates.
}
});
}
super.onPostExecute(result);
}
}
class ScanForPassesTask extends AsyncTask<Void, Void, Void> {
private void search_in(String path) {
Log.i("", "search in" + path);
File dir = new File(path);
File[] files = dir.listFiles();
for (File file : files) {
if (file.isDirectory())
search_in(file.toString());
else if (file.getName().endsWith(".pkpass")) {
Log.i("TicketViewer", "found" + file.getAbsolutePath());
new ImportAndShowAsyncTask(TicketListActivity.this, Uri.parse("file://" + file.getAbsolutePath())).execute();
}
}
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid); //To change body of overridden methods use File | Settings | File Templates.
setSupportProgressBarIndeterminateVisibility(false);
}
@Override
protected Void doInBackground(Void... params) {
search_in("/sdcard");
return null;
}
}
@Override
protected void onResume() {
super.onResume();
passes = new File(TicketDefinitions.getPassesDir(this)).list(new DirFilter());
passadapter.notifyDataSetChanged();
}
class SearchForFilesDialog extends Dialog {
public SearchForFilesDialog(Context context) {
super(context);
}
}
class DirFilter implements FilenameFilter {
@Override
public boolean accept(File dir, String filename) {
return dir.isDirectory();
}
}
class PassAdapter extends BaseAdapter {
@Override
public int getCount() {
return passes.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String mPath = path + "/" + passes[position];
View res = inflater.inflate(R.layout.pass_list_item, null);
TextView tv = (TextView) res.findViewById(R.id.label);
TextView more_tv = (TextView) res.findViewById(R.id.descr);
try {
JSONObject pass_json = new JSONObject(FileHelper.file2String(new File(mPath + "/pass.json")));
tv.setText(pass_json.getString("description"));
String more_str = "";
if (pass_json.has("eventTicket")) {
JSONObject eventTicket = pass_json.getJSONObject("eventTicket");
if (eventTicket.has("primaryFields")) {
JSONArray pri_arr = eventTicket.getJSONArray("primaryFields");
for (int i = 0; i < pri_arr.length(); i++) {
JSONObject sec_obj = pri_arr.getJSONObject(i);
more_str += sec_obj.getString("label") + ":" + sec_obj.getString("value") + "\n";
}
}
if (eventTicket.has("secondaryFields")) {
JSONArray sec_arr = eventTicket.getJSONArray("secondaryFields");
for (int i = 0; i < sec_arr.length(); i++) {
JSONObject sec_obj = sec_arr.getJSONObject(i);
more_str += sec_obj.getString("label") + ":" + sec_obj.getString("value") + "\n";
}
}
}
more_tv.setText(more_str);
} catch (Exception e) {
}
int size = (int) getResources().getDimension(R.dimen.pass_icon_size);
ImageView icon_img = (ImageView) res.findViewById(R.id.icon);
if (path != null) {
Bitmap ico = BitmapFactory.decodeFile(mPath + "/logo@2x.png");
if (ico == null)
ico = BitmapFactory.decodeFile(mPath + "/logo.png");
if (ico != null)
icon_img.setImageBitmap(Bitmap.createScaledBitmap(ico, size, size, false));
}
return res;
}
}
}

View file

@ -1,105 +1,106 @@
package org.ligi.ticketviewer;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.concurrent.Callable;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
public class UnzipPasscodeDialog {
public final static String TAG="TicketViewer";
public static class Decompress {
private InputStream _zipFile;
private String _location;
public final static String TAG = "TicketViewer";
public Decompress(InputStream zipFile, String location) {
_zipFile = zipFile;
_location = location;
public static class Decompress {
private InputStream _zipFile;
private String _location;
_dirChecker("");
}
public Decompress(InputStream zipFile, String location) {
_zipFile = zipFile;
_location = location;
public void unzip() {
try {
InputStream fin = _zipFile;
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
byte[] readData = new byte[1024];
while ((ze = zin.getNextEntry()) != null) {
Log.i(TAG,"Decompress" + "unzip" + _location + ze.getName());
if (ze.isDirectory()) {
_dirChecker(ze.getName());
} else {
FileOutputStream fout = new FileOutputStream(_location + ze.getName());
int i2 = zin.read(readData);
_dirChecker("");
}
while (i2 != -1) {
fout.write(readData, 0, i2);
i2 = zin.read(readData);
}
public void unzip() {
try {
InputStream fin = _zipFile;
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
byte[] readData = new byte[1024];
while ((ze = zin.getNextEntry()) != null) {
Log.i(TAG, "Decompress" + "unzip" + _location + ze.getName());
if (ze.isDirectory()) {
_dirChecker(ze.getName());
} else {
FileOutputStream fout = new FileOutputStream(_location + ze.getName());
zin.closeEntry();
fout.close();
}
int i2 = zin.read(readData);
}
zin.close();
} catch (Exception e) {
Log.e("Decompress", "unzip", e);
}
while (i2 != -1) {
fout.write(readData, 0, i2);
i2 = zin.read(readData);
}
}
zin.closeEntry();
fout.close();
}
private void _dirChecker(String dir) {
File f = new File(_location + dir);
}
zin.close();
} catch (Exception e) {
Log.e("Decompress", "unzip", e);
}
if (!f.isDirectory()) {
f.mkdirs();
}
}
}
}
/**
*
* @param activity
* @param autoclose
* - if the alert should close when connection is established
*
*/
public static void show(final InputStream ins,final String path,Activity activity, Intent intent_after_finish) {
private void _dirChecker(String dir) {
File f = new File(_location + dir);
ProgressDialog dialog = ProgressDialog.show(activity, "", "Opening the Passbook. Please wait...", true);
if (!f.isDirectory()) {
f.mkdirs();
}
}
}
class AlertDialogUpdater implements Runnable {
/**
* @param activity - if the alert should close when connection is established
*/
public static void show(final InputStream ins, final String path, Activity activity, Callable<Void> intent_after_finish) {
private ProgressDialog myProgress;
private Activity activity;
private Intent intent_after_finish;
ProgressDialog dialog = ProgressDialog.show(activity, "", "Opening the Passbook. Please wait...", true);
public AlertDialogUpdater(Activity activity, ProgressDialog progress, Intent intent_after_finish) {
this.activity = activity;
this.intent_after_finish = intent_after_finish;
myProgress = progress;
}
class AlertDialogUpdater implements Runnable {
public void run() {
new Decompress(ins, path).unzip();
myProgress.dismiss();
activity.startActivity(intent_after_finish);
activity.finish();
}
}
new Thread(new AlertDialogUpdater(activity, dialog, intent_after_finish)).start();
private ProgressDialog myProgress;
private Callable intent_after_finish;
}
public AlertDialogUpdater(Activity activity, ProgressDialog progress, Callable intent_after_finish) {
this.intent_after_finish = intent_after_finish;
myProgress = progress;
}
public void run() {
new Decompress(ins, path).unzip();
myProgress.dismiss();
try {
intent_after_finish.call();
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
// activity.startActivity(intent_after_finish);
// activity.finish();
}
}
new Thread(new AlertDialogUpdater(activity, dialog, intent_after_finish)).start();
}
}