mirror of
https://github.com/CatimaLoyalty/Android.git
synced 2026-05-18 05:36:39 -04:00
Initial pkpass support
This commit is contained in:
@@ -54,6 +54,18 @@
|
||||
android:host="@string/intent_import_card_from_url_host"
|
||||
android:pathPrefix="@string/intent_import_card_from_url_path_prefix" />
|
||||
</intent-filter>
|
||||
<intent-filter android:label="@string/intent_import_card_from_url">
|
||||
<action android:name="android.intent.action.VIEW"/>
|
||||
|
||||
<category android:name="android.intent.category.DEFAULT"/>
|
||||
<!-- Accept pkpass files -->
|
||||
<data android:mimeType="application/octet-stream"/>
|
||||
<data android:mimeType="application/zip"/>
|
||||
<data android:mimeType="application/vnd.apple.pkpass"/>
|
||||
<data android:mimeType="application/pkpass"/>
|
||||
<data android:mimeType="application/vndapplepkpass"/>
|
||||
<data android:mimeType="application/vnd-com.apple.pkpass"/>
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity
|
||||
android:name=".BarcodeSelectorActivity"
|
||||
|
||||
@@ -25,7 +25,7 @@ public class ImportURIHelper {
|
||||
shareText = context.getResources().getString(R.string.intent_import_card_from_url_share_text);
|
||||
}
|
||||
|
||||
private boolean isImportUri(Uri uri) {
|
||||
public boolean isImportUri(Uri uri) {
|
||||
return uri.getHost().equals(host) && uri.getPath().equals(path);
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,9 @@ import com.google.zxing.integration.android.IntentResult;
|
||||
import com.jaredrummler.android.colorpicker.ColorPickerDialog;
|
||||
import com.jaredrummler.android.colorpicker.ColorPickerDialogListener;
|
||||
|
||||
import org.json.JSONException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InvalidObjectException;
|
||||
|
||||
public class LoyaltyCardEditActivity extends AppCompatActivity
|
||||
@@ -58,23 +61,57 @@ public class LoyaltyCardEditActivity extends AppCompatActivity
|
||||
int loyaltyCardId;
|
||||
boolean updateLoyaltyCard;
|
||||
Uri importLoyaltyCardUri = null;
|
||||
String importLoyaltyCardType = null;
|
||||
Integer headingColorValue = null;
|
||||
Integer headingStoreTextColorValue = null;
|
||||
|
||||
DBHelper db;
|
||||
ImportURIHelper importUriHelper;
|
||||
PkpassImporter pkpassImporter;
|
||||
|
||||
private void extractIntentFields(Intent intent)
|
||||
{
|
||||
final Bundle b = intent.getExtras();
|
||||
loyaltyCardId = b != null ? b.getInt("id") : 0;
|
||||
updateLoyaltyCard = b != null && b.getBoolean("update", false);
|
||||
importLoyaltyCardType = intent.getType();
|
||||
importLoyaltyCardUri = intent.getData();
|
||||
|
||||
Log.d(TAG, "View activity: id=" + loyaltyCardId
|
||||
+ ", updateLoyaltyCard=" + Boolean.toString(updateLoyaltyCard));
|
||||
}
|
||||
|
||||
private LoyaltyCard importCard(String type, Uri uri)
|
||||
{
|
||||
// Pkpass
|
||||
if(type != null && pkpassImporter.isPkpass(type))
|
||||
{
|
||||
try
|
||||
{
|
||||
return pkpassImporter.fromURI(uri);
|
||||
}
|
||||
catch (IOException | JSONException ex)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Import URI
|
||||
if(importUriHelper.isImportUri(uri))
|
||||
{
|
||||
try
|
||||
{
|
||||
return importUriHelper.parse(importLoyaltyCardUri);
|
||||
}
|
||||
catch (InvalidObjectException ex)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState)
|
||||
{
|
||||
@@ -93,6 +130,7 @@ public class LoyaltyCardEditActivity extends AppCompatActivity
|
||||
|
||||
db = new DBHelper(this);
|
||||
importUriHelper = new ImportURIHelper(this);
|
||||
pkpassImporter = new PkpassImporter(this);
|
||||
|
||||
storeFieldEdit = findViewById(R.id.storeNameEdit);
|
||||
noteFieldEdit = findViewById(R.id.noteEdit);
|
||||
@@ -186,10 +224,10 @@ public class LoyaltyCardEditActivity extends AppCompatActivity
|
||||
else if(importLoyaltyCardUri != null)
|
||||
{
|
||||
// Try to parse
|
||||
LoyaltyCard importCard;
|
||||
try {
|
||||
importCard = importUriHelper.parse(importLoyaltyCardUri);
|
||||
} catch (InvalidObjectException ex) {
|
||||
LoyaltyCard importCard = importCard(importLoyaltyCardType, importLoyaltyCardUri);
|
||||
|
||||
if(importCard == null)
|
||||
{
|
||||
Toast.makeText(this, R.string.failedParsingImportUriError, Toast.LENGTH_LONG).show();
|
||||
finish();
|
||||
return;
|
||||
|
||||
60
app/src/main/java/protect/card_locker/PkpassImporter.java
Normal file
60
app/src/main/java/protect/card_locker/PkpassImporter.java
Normal file
@@ -0,0 +1,60 @@
|
||||
package protect.card_locker;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
public class PkpassImporter {
|
||||
Context context;
|
||||
|
||||
public PkpassImporter(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public boolean isPkpass(String type) {
|
||||
return Arrays.asList("application/octet-stream", "application/zip", "application/vnd.apple.pkpass", "application/pkpass", "application/vndapplepkpass", "application/vnd-com.apple.pkpass").contains(type);
|
||||
}
|
||||
|
||||
public LoyaltyCard fromURI(Uri uri) throws IOException, JSONException {
|
||||
ZipInputStream zipInputStream = new ZipInputStream(context.getContentResolver().openInputStream(uri));
|
||||
|
||||
ZipEntry entry;
|
||||
|
||||
while((entry = zipInputStream.getNextEntry()) != null)
|
||||
{
|
||||
if(!entry.getName().equals("pass.json"))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for(int c = zipInputStream.read(); c != -1; c = zipInputStream.read())
|
||||
{
|
||||
sb.append((char) c);
|
||||
}
|
||||
|
||||
String readData = sb.toString();
|
||||
|
||||
JSONObject json = new JSONObject(readData);
|
||||
|
||||
String store = json.getString("description");
|
||||
// TODO: Note
|
||||
String cardId = json.getJSONObject("barcode").getString("message");
|
||||
String barcodeType = json.getJSONObject("barcode").getString("format").substring("PKBarcodeFormat".length());
|
||||
if(barcodeType.equals("QR"))
|
||||
{
|
||||
barcodeType = "QR_CODE";
|
||||
}
|
||||
return new LoyaltyCard(-1, store, "", cardId, barcodeType, null, null);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user