Created
October 8, 2015 15:09
-
-
Save wpride/61ac27dc6f1a5ffaebc0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dimagi.test.external; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.os.Parcel; | |
import android.util.Pair; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.Button; | |
import android.widget.Toast; | |
import java.security.GeneralSecurityException; | |
import java.security.KeyFactory; | |
import java.security.SecureRandom; | |
import java.security.interfaces.RSAPublicKey; | |
import java.security.spec.KeySpec; | |
import java.security.spec.X509EncodedKeySpec; | |
import javax.crypto.Cipher; | |
import javax.crypto.KeyGenerator; | |
import javax.crypto.SecretKey; | |
/** | |
* Non functional class to show how to use the Key Request, Case Content Provider, and Broadcast Receiver | |
* API functions of CommCare. The API code is all correct, but any layout/UI components will need | |
* to be wired into an actual layout (IE you will need a layout.main class with button for the key request | |
* and case DB read calls) | |
*/ | |
public class ExternalAppActivity extends Activity { | |
byte[] publicKey; | |
String keyId; | |
public static final int KEY_REQUEST_CODE = 1; | |
/* | |
* (non-Javadoc) | |
* @see android.app.Activity#onCreate(android.os.Bundle) | |
*/ | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
Button ac = (Button)this.findViewById(R.id.acquire_key); | |
ac.setOnClickListener(new OnClickListener() { | |
/** | |
* When you click this button you will request permission from CommCare to read from | |
* its encrypted DB. This will launch CommCare and prompt the user with the option | |
* to give permission or not | |
*/ | |
public void onClick(View v) { | |
Intent i = new Intent("org.commcare.dalvik.action.CommCareKeyAccessRequest"); | |
ExternalAppActivity.this.startActivityForResult(i, KEY_REQUEST_CODE); | |
} | |
}); | |
} | |
/* (non-Javadoc) | |
* @see android.app.Activity#onActivityResult(int, int, android.content.Intent) | |
*/ | |
/** | |
* This method will be called when we return from CommCare. We save the public key so that | |
* we can decrypt the database latyer. | |
*/ | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
if(requestCode == KEY_REQUEST_CODE) { | |
if(resultCode ==Activity.RESULT_OK) { | |
keyId = data.getStringExtra("commcare_sharing_key_id"); | |
publicKey = data.getByteArrayExtra("commcare_sharing_key_payload"); | |
} else { | |
Toast.makeText(this, "Key Request Denied!", Toast.LENGTH_LONG).show(); | |
} | |
} | |
} | |
/** | |
* We call this method to read from the CommCare case database. This launches a URI query | |
* that CommCare will handle. We can then iterate over the cursor contents. | |
*/ | |
protected void showCaseData(String selection, String[] selectionArgs) { | |
ListView la = (ListView)this.findViewById(R.id.list_view); | |
Cursor cursor = this.managedQuery(Uri.parse("content://org.commcare.dalvik.case/casedb/case"), null, selection, selectionArgs, null); | |
if (cursor.moveToFirst()){ | |
do{ | |
String data = cursor.getString(cursor.getColumnIndex("data")); | |
// do what ever you want here | |
}while(cursor.moveToNext()); | |
} | |
cursor.close(); | |
} | |
// Broadcast Receiving | |
static ArrayList<String> broadcasts; | |
static BroadcastReceiver receiver; | |
/** | |
* This method will start this activity listening for the "data.update" action from CommCare | |
* which will be fired whenever commcare updates its database. the onReceive method will be called, | |
* so we can trigger any application updates in that method (IE read from the case database) | |
*/ | |
private void startListening() { | |
synchronized(listening) { | |
stopListening(); | |
this.listening[0] = true; | |
broadcasts = new ArrayList<String>(); | |
receiver = new BroadcastReceiver() { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
synchronized(listening) { | |
String broadcast = "From CommCare (" + new SimpleDateFormat("HH:mm:ss", Locale.US).format(new Date()) + "): " + intent.getAction(); | |
broadcasts.add(broadcast); | |
} | |
IntentReceiverTest.this.updateState(); | |
} | |
}; | |
broadcasts = new ArrayList<String>(); | |
IntentFilter filter = new IntentFilter(); | |
filter.addAction("org.commcare.dalvik.api.action.data.update"); | |
this.registerReceiver(receiver, filter); | |
IntentReceiverTest.this.updateState(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment