Skip to content

Commit

Permalink
multiple updates for performance, stability and bug fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
n8fr8 committed Dec 19, 2023
1 parent a008e0c commit f45f702
Show file tree
Hide file tree
Showing 15 changed files with 212 additions and 42 deletions.
4 changes: 3 additions & 1 deletion .idea/assetWizardSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class C2paUtils {
private const val C2PA_CERT_PATH_PARENT = "crp.cert"
private const val C2PA_KEY_PATH_PARENT = "crp.key"

private const val CERT_VALIDITY_DAYS = 365U //1 year
private const val CERT_VALIDITY_DAYS = 1825U //5 years

private var _identityUri = "https://proofmode.org"
private var _identityName = "ProofMode"
Expand Down Expand Up @@ -148,32 +148,21 @@ class C2paUtils {
*/
fun initCredentials (mContext : Context, emailAddress: String?, pgpFingerprint: String?) {

emailAddress?.let {
_identityEmail = emailAddress
}
if (emailAddress?.isNotEmpty() == true)
_identityEmail = emailAddress

pgpFingerprint?.let {
if (pgpFingerprint?.isNotEmpty() == true)
_identityKey = pgpFingerprint
}


var fileUserCert = File(mContext.filesDir, C2PA_CERT_PATH)
var fileUserKey = File(mContext.filesDir, C2PA_KEY_PATH)

var fileParentCert = File(mContext.filesDir, C2PA_CERT_PATH_PARENT)
var fileParentKey = File(mContext.filesDir,C2PA_KEY_PATH_PARENT)

var userKey : FileData

if (!fileUserKey.exists()) {
userKey = createPrivateKey()
fileUserKey.writeBytes(userKey.getBytes())
}
else
{
userKey = FileData(fileUserKey.absolutePath,fileUserKey.readBytes(),fileUserKey.name)
}
if ((!fileUserKey.exists()) || (!fileUserCert.exists())) {

if (!fileUserCert.exists()) {

var parentKey = createPrivateKey();
fileParentKey.writeBytes(parentKey.getBytes())
Expand All @@ -185,6 +174,9 @@ class C2paUtils {

fileParentCert.writeBytes(rootCert.getCertificateBytes())

var userKey = createPrivateKey()
fileUserKey.writeBytes(userKey.getBytes())

var userCertType =
CertificateType.ContentCredentials("ProofMode-User-$_identityKey", CERT_VALIDITY_DAYS)
var userCertOptions = CertificateOptions(
Expand All @@ -208,16 +200,17 @@ class C2paUtils {
}
else
{
var fileDataParentKey = FileData(fileParentKey.absolutePath,fileParentKey.readBytes(),fileParentKey.name)
var parentCert = Certificate(FileData(fileParentCert.absolutePath,fileParentCert.readBytes(),fileParentCert.name), fileDataParentKey, null)
userCert = Certificate(FileData(fileUserCert.absolutePath,fileUserCert.readBytes(),fileUserKey.name), userKey, parentCert)
var userPrivateKey = FileData(fileUserKey.absolutePath,fileUserKey.readBytes(),fileUserKey.name)
var parentPrivateKey = FileData(fileParentKey.absolutePath,fileParentKey.readBytes(),fileParentKey.name)
var parentCert = Certificate(FileData(fileParentCert.absolutePath,fileParentCert.readBytes(),fileParentCert.name), parentPrivateKey, null)
userCert = Certificate(FileData(fileUserCert.absolutePath,fileUserCert.readBytes(),fileUserKey.name), userPrivateKey, parentCert)
}
}

/**
* add new C2PA Content Credential assertions and then embed and sign them
*/
fun addContentCredentials(mContext : Context, emailAddress: String, pgpFingerprint: String, emailDisplay: String, webLink: String, isDirectCapture: Boolean, allowMachineLearning: Boolean, fileImageIn: File, fileImageOut: File) {
private fun addContentCredentials(mContext : Context, emailAddress: String, pgpFingerprint: String, emailDisplay: String, webLink: String, isDirectCapture: Boolean, allowMachineLearning: Boolean, fileImageIn: File, fileImageOut: File) {

if (userCert == null)
initCredentials(mContext, emailAddress, pgpFingerprint)
Expand Down Expand Up @@ -248,9 +241,21 @@ class C2paUtils {
else
contentCreds?.addPermissiveAiTrainingAssertions()

contentCreds?.addEmailAssertion(emailAddress, emailDisplay)
// contentCreds?.addPgpAssertion(pgpFingerprint, pgpFingerprint)
// contentCreds?.addWebsiteAssertion(webLink)
// contentCreds?.addEmailAssertion(emailAddress, emailDisplay) //not yet implemented

/**
* ///not yet working
contentCreds?.addJsonAssertion("stds.schema-org.CreativeWork","\n" +
"\"@context\": \"http://schema.org/\"," +
"\"@type\": \"CreativeWork\"," +
"\"author\": [" +
"\"@type\": \"Person\"," +
"\"name\": \"$emailDisplay\"\n}]," +
"\"copyrightNotice\": \"$emailDisplay 2023\"")
**/

contentCreds?.addPgpAssertion(pgpFingerprint, pgpFingerprint)
contentCreds?.addWebsiteAssertion(webLink)

var exifMake = Build.MANUFACTURER
var exifModel = Build.MODEL
Expand Down Expand Up @@ -285,7 +290,6 @@ class C2paUtils {

contentCreds?.embedManifest(fileImageOut.absolutePath)


}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Looper;
import android.provider.Settings;

import androidx.core.app.ActivityCompat;
Expand Down Expand Up @@ -71,10 +72,11 @@ public void updateLocation () {
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

/**
if (isGPSEnabled)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,60000,0,this);
else if (isNetworkEnabled)
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,60000,0,this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,60000,0,this);**/
}

public void stopUpdateLocation () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package org.witness.proofmode.camera.fragments

import android.R.attr.data
import android.annotation.SuppressLint
import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.database.Cursor
import android.hardware.display.DisplayManager
import android.net.Uri
import android.os.Build
Expand Down Expand Up @@ -54,6 +52,7 @@ import org.witness.proofmode.camera.utils.*
import java.io.File
import java.io.FileNotFoundException
import java.util.concurrent.ExecutionException
import java.util.concurrent.Executors
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min
Expand Down Expand Up @@ -602,6 +601,9 @@ class CameraFragment : BaseFragment<FragmentCameraBinding>(R.layout.fragment_cam
captureImage()
}


private val mExec = Executors.newFixedThreadPool(1)

private fun captureImage() {
val localImageCapture =
imageCapture ?: throw IllegalStateException("Camera initialization failed.")
Expand Down Expand Up @@ -638,7 +640,7 @@ class CameraFragment : BaseFragment<FragmentCameraBinding>(R.layout.fragment_cam

localImageCapture.takePicture(
outputOptions, // the options needed for the final image
requireContext().mainExecutor(), // the executor, on which the task will run
mExec, // the executor, on which the task will run
object : OnImageSavedCallback { // the callback, about the result of capture process
override fun onImageSaved(outputFileResults: OutputFileResults) {

Expand Down Expand Up @@ -749,7 +751,7 @@ class CameraFragment : BaseFragment<FragmentCameraBinding>(R.layout.fragment_cam

var intent = Intent(NEW_MEDIA_EVENT)
intent.data = newMediaFile
LocalBroadcastManager.getInstance(requireContext()).sendBroadcast(intent)
activity?.applicationContext?.let { LocalBroadcastManager.getInstance(it).sendBroadcast(intent) }

}

Expand Down
5 changes: 5 additions & 0 deletions android-libproofcam/src/main/res/drawable/cricon.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="64dp" android:viewportHeight="128"
android:viewportWidth="128" android:width="64dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#ffffff" android:pathData="M0,64C0,28.65 28.65,0 64,0V0C99.35,0 128,28.65 128,64V128H64C28.65,128 0,99.35 0,64V64Z"/>
<path android:fillColor="#000000" android:fillType="evenOdd" android:pathData="M116.91,64V116.91H64C34.78,116.91 11.09,93.22 11.09,64C11.09,34.78 34.78,11.09 64,11.09C93.22,11.09 116.91,34.78 116.91,64ZM0,64C0,28.65 28.65,0 64,0C99.35,0 128,28.65 128,64V128H64C28.65,128 0,99.35 0,64ZM25.44,66.53C25.44,79.74 34.31,90.87 48.51,90.87C60.18,90.87 68.05,83.18 69.95,73.14H58.46C57.01,77.75 53.3,80.55 48.51,80.55C41.36,80.55 36.66,74.95 36.66,66.53C36.66,58.12 41.36,52.51 48.51,52.51C53.12,52.51 56.74,55.14 58.28,59.48H69.86C67.78,49.71 60,42.2 48.51,42.2C34.31,42.2 25.44,53.33 25.44,66.53ZM85.37,43.47H74.52V89.6H85.83V65.54C85.83,61.02 87.09,58.03 89.26,56.22C91.16,54.5 93.7,53.6 97.77,53.6H100.66V42.93H97.86C91.89,42.93 87.91,45.1 85.37,48.44V43.47Z"/>
</vector>
4 changes: 4 additions & 0 deletions android-libproofcam/src/main/res/drawable/cricon_off.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<vector android:height="64dp" android:viewportHeight="128"
android:viewportWidth="128" android:width="64dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#ffffff" android:fillType="evenOdd" android:pathData="M116.91,64V116.91H64C34.78,116.91 11.09,93.22 11.09,64C11.09,34.78 34.78,11.09 64,11.09C93.22,11.09 116.91,34.78 116.91,64ZM0,64C0,28.65 28.65,0 64,0C99.35,0 128,28.65 128,64V128H64C28.65,128 0,99.35 0,64ZM25.44,66.53C25.44,79.74 34.31,90.87 48.51,90.87C60.18,90.87 68.05,83.18 69.95,73.14H58.46C57.01,77.75 53.3,80.55 48.51,80.55C41.36,80.55 36.66,74.95 36.66,66.53C36.66,58.12 41.36,52.51 48.51,52.51C53.12,52.51 56.74,55.14 58.28,59.48H69.86C67.78,49.71 60,42.2 48.51,42.2C34.31,42.2 25.44,53.33 25.44,66.53ZM85.37,43.47H74.52V89.6H85.83V65.54C85.83,61.02 87.09,58.03 89.26,56.22C91.16,54.5 93.7,53.6 97.77,53.6H100.66V42.93H97.86C91.89,42.93 87.91,45.1 85.37,48.44V43.47Z"/>
</vector>
3 changes: 3 additions & 0 deletions android-libproofcam/src/main/res/layout/camera_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />



</FrameLayout>
5 changes: 4 additions & 1 deletion android-libproofcam/src/main/res/layout/fragment_camera.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


<ImageButton
android:id="@+id/btnTimer"
android:layout_width="@dimen/button_size_small"
Expand Down Expand Up @@ -167,7 +168,9 @@
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btnFlash"
app:layout_constraintTop_toTopOf="@id/btnFlash"
tools:ignore="ContentDescription" />
tools:ignore="ContentDescription"
android:visibility="gone"
/>

<ImageButton
android:id="@+id/btnExposure"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,11 @@ public class PgpUtils {
private PGPSecretKeyRing skr = null;
private PGPPublicKeyRing pkr = null;

private final static String keyId = "[email protected]";
private static String keyEmail = "[email protected]";

private final static String FILE_SECRET_KEY_RING = "pkr.asc";
private final static String FILE_PUBLIC_KEY_RING = "pub.asc";

//public final static String DEFAULT_PASSWORD = "password"; //static string for local keystore
private final static String URL_POST_KEY_ENDPOINT = "https://keys.openpgp.org/vks/v1/upload";

public final static String URL_LOOKUP_ENDPOINT = "https://keys.openpgp.org/search?q=0x";
Expand Down Expand Up @@ -308,6 +307,18 @@ public static boolean keyRingExists (Context context)
return fileSecKeyRing.exists() && filePubKeyRing.exists();
}

public void resetCrypto (Context context) {
File fileSecKeyRing = new File(context.getFilesDir(),FILE_SECRET_KEY_RING);
File filePubKeyRing = new File(context.getFilesDir(),FILE_PUBLIC_KEY_RING);
fileSecKeyRing.delete();
filePubKeyRing.delete();
pgpSec = null;
}

public static void setKeyid (String email) {
keyEmail = email;
}

public synchronized void initCrypto (Context context, String password) throws IOException, PGPException {
if (pgpSec == null) {

Expand All @@ -329,7 +340,7 @@ public synchronized void initCrypto (Context context, String password) throws IO
if (password.isEmpty())
throw new IOException("Empty PGP Key password not allowed for key generation");

final PGPKeyRingGenerator krgen = generateKeyRingGenerator(keyId, password.toCharArray());
final PGPKeyRingGenerator krgen = generateKeyRingGenerator(keyEmail, password.toCharArray());
skr = krgen.generateSecretKeyRing();

ArmoredOutputStream sout = new ArmoredOutputStream((new FileOutputStream(fileSecKeyRing)));
Expand Down
38 changes: 35 additions & 3 deletions app/src/main/java/org/witness/proofmode/Activities.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import androidx.lifecycle.viewModelScope
import androidx.room.ColumnInfo
import androidx.room.Dao
import androidx.room.Database
import androidx.room.Delete
import androidx.room.DeleteTable
import androidx.room.Entity
import androidx.room.Insert
import androidx.room.PrimaryKey
Expand Down Expand Up @@ -139,6 +141,12 @@ interface ActivitiesDao {
@Insert
suspend fun insert(activity: Activity?)

@Delete
suspend fun delete(activity: Activity?)

@Query("DELETE FROM activities WHERE data LIKE '%\"' || :id || '\"%'")
suspend fun deleteId(id: String?)

@Query("SELECT * FROM activities WHERE data LIKE '%\"' || :id || '\"%' LIMIT 1")
suspend fun activityFromProofableItemId(id: String): Activity?
}
Expand Down Expand Up @@ -211,19 +219,43 @@ object Activities: ViewModel()

fun addActivity(activity: Activity, context: Context) {
val db = getDB(context)

val lastActivity = this.activities.lastOrNull()
if (activity.type is ActivityType.MediaCaptured && lastActivity != null && lastActivity.type is ActivityType.MediaCaptured && (lastActivity.startTime.time + timeBatchWindow) >= activity.startTime.time ) {
if (activity.type is ActivityType.MediaCaptured && lastActivity != null && lastActivity.type is ActivityType.MediaCaptured && (lastActivity.startTime.time + timeBatchWindow) >= activity.startTime.time) {
// If within the same minute, add it to the same "batch" as the previous one.
lastActivity.type.items += activity.type.items
viewModelScope.launch {
db.activitiesDao().update(lastActivity)
if (db.activitiesDao().activityFromProofableItemId(activity.id) == null)
db.activitiesDao().update(lastActivity)
}
} else {
this.activities.add(activity)
viewModelScope.launch {
db.activitiesDao().insert(activity)
if (db.activitiesDao().activityFromProofableItemId(activity.id) == null)
db.activitiesDao().insert(activity)
}
}

}

fun clearActivity (id: String, context: Context) {
val db = getDB(context)
viewModelScope.launch {

var activity = db.activitiesDao().activityFromProofableItemId(id)

activities.remove(activity)

activity?.let {
db.activitiesDao().delete(activity)
db.activitiesDao().deleteId(activity.id)
}

activities.clear()
activities.addAll(db.activitiesDao().getAll())
}


}

private fun getActivityProofableItems(activity: Activity): SnapshotStateList<ProofableItem> {
Expand Down
Loading

0 comments on commit f45f702

Please sign in to comment.