update works - but needs some finishing

This commit is contained in:
ligi 2014-10-26 18:18:22 +01:00
parent 9b97a85cef
commit 5c46e4a8af
7 changed files with 185 additions and 27 deletions

View file

@ -125,6 +125,11 @@ public class AppleStylePassReader {
}
}
pass.setSerial(readJsonSafeAsOptional(pass_json,"serialNumber"));
pass.setAuthToken(readJsonSafeAsOptional(pass_json,"authenticationToken"));
pass.setWebserviceURL(readJsonSafeAsOptional(pass_json,"webServiceURL"));
pass.setPassTypeIdent(readJsonSafeAsOptional(pass_json,"passTypeIdentifier"));
final List<PassLocation> locations = new ArrayList<>();
try {
@ -147,22 +152,27 @@ public class AppleStylePassReader {
}
pass.setLocations(locations);
try {
String backgroundColor = pass_json.getString("backgroundColor");
pass.setBackgroundColor(parseColor(backgroundColor, 0));
} catch (JSONException e) {
}
try {
String foregroundColor = pass_json.getString("foregroundColor");
pass.setForegroundColor(parseColor(foregroundColor, 0xffffffff));
} catch (JSONException e) {
}
readJsonSafe(pass_json, "backgroundColor", new JsonStringReadCallback() {
@Override
public void onString(String string) {
pass.setBackgroundColor(parseColor(string, 0));
}
});
try {
pass.setDescription(translation.translate(pass_json.getString("description")));
} catch (JSONException e) {
}
readJsonSafe(pass_json, "foregroundColor", new JsonStringReadCallback() {
@Override
public void onString(String string) {
pass.setForegroundColor(parseColor(string, 0xffffffff));
}
});
readJsonSafe(pass_json, "description", new JsonStringReadCallback() {
@Override
public void onString(String string) {
pass.setDescription(translation.translate(string));
}
});
// try to find in a predefined set of tickets
@ -232,6 +242,31 @@ public class AppleStylePassReader {
return Optional.absent();
}
interface JsonStringReadCallback {
void onString(String string);
}
private static Optional<String> readJsonSafeAsOptional(JSONObject json, String key) {
if (json.has(key)) {
try {
return Optional.of(json.getString(key));
} catch (JSONException e) {
// some passes just do not have the field
}
}
return Optional.absent();
}
private static void readJsonSafe(JSONObject json, String key, JsonStringReadCallback callback) {
if (json.has(key)) {
try {
callback.onString(json.getString(key));
} catch (JSONException e) {
// some passes just do not have the field
}
}
}
private static String findBitmapFile(String path, Optional<String> localizedPath, String bitmap) {
String res;
if (localizedPath.isPresent()) {
@ -305,8 +340,8 @@ public class AppleStylePassReader {
private static int parseColorRGBStyle(String color_str, int defaultValue) {
Pattern pattern = Pattern.compile("rgb *\\( *([0-9]+), *([0-9]+), *([0-9]+) *\\)");
Matcher matcher = pattern.matcher(color_str);
final Pattern pattern = Pattern.compile("rgb *\\( *([0-9]+), *([0-9]+), *([0-9]+) *\\)");
final Matcher matcher = pattern.matcher(color_str);
if (matcher.matches()) {
return (255 << 24 |

View file

@ -58,4 +58,13 @@ public interface Pass extends Serializable {
Optional<BarCode> getBarCode();
Optional<Bitmap> getStripImage();
Optional<String> getWebServiceURL();
Optional<String> getAuthToken();
Optional<String> getSerial();
Optional<String> getPassIdent();
}

View file

@ -41,8 +41,12 @@ public class PassImpl implements Pass, Serializable {
private Optional<String> logoBitmapFile;
private Optional<String> stripBitmapFile;
public static final String[] TYPES = new String[]{"coupon", "eventTicket", "boardingPass", "generic", "storeCard"};
private Optional<String> authToken;
private Optional<String> webServiceURL;
private Optional<String> serial;
public static final String[] TYPES = new String[]{"coupon", "eventTicket", "boardingPass", "generic", "storeCard"};
private Optional<String> passTypeIdent;
@Override
public Optional<String> getOrganisation() {
@ -160,6 +164,25 @@ public class PassImpl implements Pass, Serializable {
return getBitmapFromOptionalString(stripBitmapFile);
}
@Override
public Optional<String> getWebServiceURL() {
return webServiceURL;
}
@Override
public Optional<String> getAuthToken() {
return authToken;
}
@Override
public Optional<String> getSerial() {
return serial;
}
@Override
public Optional<String> getPassIdent() {
return passTypeIdent;
}
public Optional<Bitmap> getLogoBitmap() {
return getBitmapFromOptionalString(logoBitmapFile);
@ -272,4 +295,20 @@ public class PassImpl implements Pass, Serializable {
public void setBarCode(BarCode barCode) {
this.barCode = Optional.fromNullable(barCode);
}
public void setSerial(Optional<String> serial) {
this.serial = serial;
}
public void setAuthToken(Optional<String> authToken) {
this.authToken = authToken;
}
public void setWebserviceURL(Optional<String> webServiceURL) {
this.webServiceURL = webServiceURL;
}
public void setPassTypeIdent(Optional<String> passTypeIdent) {
this.passTypeIdent = passTypeIdent;
}
}

View file

@ -208,12 +208,14 @@ public class PassViewActivity extends PassViewActivityBase {
public boolean onPrepareOptionsMenu(Menu menu) {
boolean res = super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.menu_map).setVisible((optionalPass.isPresent() && optionalPass.get().isValid() && optionalPass.get().getLocations().size() > 0));
menu.findItem(R.id.menu_update).setVisible((optionalPass.isPresent() && optionalPass.get().isValid() && optionalPass.get().getAuthToken().isPresent() && optionalPass.get().getSerial().isPresent() && optionalPass.get().getAuthToken().isPresent() && optionalPass.get().getPassIdent().isPresent()));
return res;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.map_item, menu);
getMenuInflater().inflate(R.menu.update, menu);
return super.onCreateOptionsMenu(menu);
}

View file

@ -1,19 +1,27 @@
package org.ligi.passandroid.ui;
import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
import com.google.common.base.Optional;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import org.ligi.passandroid.App;
import org.ligi.passandroid.R;
import org.ligi.passandroid.Tracker;
import org.ligi.passandroid.model.InputStreamWithSource;
import org.ligi.passandroid.model.Pass;
import java.io.IOException;
public class PassViewActivityBase extends ActionBarActivity {
public Optional<Pass> optionalPass;
@ -46,22 +54,78 @@ public class PassViewActivityBase extends ActionBarActivity {
return true;
}
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
if (item.getItemId() == R.id.menu_light) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
case R.id.menu_light:
setToFullBrightness();
return true;
Window win = getWindow();
WindowManager.LayoutParams params = win.getAttributes();
params.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL;
win.setAttributes(params);
case R.id.menu_update:
new Thread(new UpdateAsync()).start();
return true;
return true;
}
return super.onOptionsItemSelected(item);
}
class UpdateAsync implements Runnable {
@Override
public void run() {
final OkHttpClient client = new OkHttpClient();
final Pass pass = optionalPass.get();
final String url = pass.getWebServiceURL().get() + "/v1/passes/" + pass.getPassIdent().get() + "/" + pass.getSerial().get();
final Request.Builder requestBuilder = new Request.Builder().url(url);
requestBuilder.addHeader("Authorization", "ApplePass " + pass.getAuthToken().get());
final Request request = requestBuilder.build();
final Response response;
try {
response = client.newCall(request).execute();
final InputStreamWithSource inputStreamWithSource = new InputStreamWithSource(url, response.body().byteStream());
UnzipPassController.processInputStream(new UnzipPassController.InputStreamUnzipControllerSpec(inputStreamWithSource, PassViewActivityBase.this, new UnzipPassController.SuccessCallback() {
@Override
public void call(String pathToPassbook) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(PassViewActivityBase.this, "Pass Updated", Toast.LENGTH_LONG).show();
}
});
}
}, new UnzipPassController.FailCallback() {
@Override
public void fail(final String reason) {
runOnUiThread(new Runnable() {
@Override
public void run() {
new AlertDialog.Builder(PassViewActivityBase.this).setMessage("Could not update pass :( " + reason + ")").setPositiveButton(android.R.string.ok, null).show();
}
});
}
}));
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void setToFullBrightness() {
final Window win = getWindow();
final WindowManager.LayoutParams params = win.getAttributes();
params.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL;
win.setAttributes(params);
}
}

View file

@ -113,6 +113,7 @@ public class UnzipPassController {
public final SuccessCallback onSuccessCallback;
public final FailCallback failCallback;
public String targetPath;
public boolean overwrite=false;
public UnzipControllerSpec(String targetPath, Context context, SuccessCallback onSuccessCallback, FailCallback failCallback) {
this.context = context;

View file

@ -0,0 +1,8 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_update"
yourapp:showAsAction="ifRoom"
android:title="update"
android:icon="@android:drawable/ic_popup_sync" />
</menu>