Skip to content

Commit

Permalink
tweak main activity and camera shortcut loading for stability and per…
Browse files Browse the repository at this point in the history
…formance
  • Loading branch information
n8fr8 committed Sep 19, 2024
1 parent e83e90d commit e852d7f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 39 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
android:exported="true"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:launchMode="singleInstance"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
113 changes: 74 additions & 39 deletions app/src/main/java/org/witness/proofmode/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte

private val ACTION_OPEN_CAMERA = "org.witness.proofmode.OPEN_CAMERA"


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -78,14 +79,56 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
)
}

mPrefs = PreferenceManager.getDefaultSharedPreferences(this)

val isOn = mPrefs.getBoolean("doProof", false)

if (isOn)
(application as ProofModeApp).init(this)

val intentFilter = IntentFilter("org.witness.proofmode.NEW_MEDIA")
intentFilter.apply {
addDataType("image/*")
addDataType("video/*")
}

ContextCompat.registerReceiver(this,
cameraReceiver, intentFilter,
ContextCompat.RECEIVER_EXPORTED
)

ContextCompat.registerReceiver(this,
cameraReceiver, IntentFilter(EVENT_PROOF_GENERATED),
ContextCompat.RECEIVER_EXPORTED
)

ContextCompat.registerReceiver(this,
cameraReceiver, IntentFilter(INTENT_ACTIVITY_ITEMS_SHARED),
ContextCompat.RECEIVER_EXPORTED
)

initUI()

if (intent?.action == ACTION_OPEN_CAMERA)
{
openCamera()
}
else
{

}



}

private fun initUI () {

mainBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(mainBinding.root)
val toolbar = mainBinding.toolbar
setSupportActionBar(toolbar)
supportActionBar?.title = ""
mPrefs = PreferenceManager.getDefaultSharedPreferences(this)
layoutOn = mainBinding.contentMain.layoutOn
layoutOff = mainBinding.contentMain.layoutOff
if (mPrefs.getBoolean("firsttime", true)) {
Expand All @@ -104,9 +147,8 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte

var switchItem = navigationView.menu.findItem(R.id.menu_background_service);
var switchView = MenuItemCompat.getActionView(switchItem) as CompoundButton
val isOn = mPrefs.getBoolean("doProof", false)

switchView.isChecked = isOn
switchView.isChecked = mPrefs.getBoolean("doProof", false)

switchView.setOnCheckedChangeListener { buttonView, isChecked ->
setProofModeOn(isChecked)
Expand All @@ -130,50 +172,28 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
// Showing the popup menu
popupMenu.show()
}

//updateOnOffState(false)

if (isOn)
(application as ProofModeApp).init(this)

// Setup activity view
val activityView = findViewById<ComposeView>(R.id.activityView)
activityView.setContent {
ActivitiesView {
itemsSelected(it)
}
}

val intentFilter = IntentFilter("org.witness.proofmode.NEW_MEDIA")
intentFilter.apply {
addDataType("image/*")
addDataType("video/*")
fab = findViewById<FloatingActionButton>(R.id.fab)
fab.setOnClickListener { view ->
openCamera()
}

ContextCompat.registerReceiver(this,
cameraReceiver, intentFilter,
ContextCompat.RECEIVER_EXPORTED
)

ContextCompat.registerReceiver(this,
cameraReceiver, IntentFilter(EVENT_PROOF_GENERATED),
ContextCompat.RECEIVER_EXPORTED
)

ContextCompat.registerReceiver(this,
cameraReceiver, IntentFilter(INTENT_ACTIVITY_ITEMS_SHARED),
ContextCompat.RECEIVER_EXPORTED
)
// Setup activity view
val activityView = findViewById<ComposeView>(R.id.activityView)

/**
Activities.load(this)
fab = findViewById<FloatingActionButton>(R.id.fab)
fab.setOnClickListener { view ->
openCamera()
}
}**/

if (intent?.action == ACTION_OPEN_CAMERA)
{
openCamera()
activityView.setContent {
ActivitiesView {
itemsSelected(it)
}
}
}

Expand Down Expand Up @@ -316,7 +336,13 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte

override fun onResume() {
super.onResume()
// updateOnOffState(false)

if (intent?.action != ACTION_OPEN_CAMERA) {
if (!activitiesLoaded) {
Activities.load(this)
activitiesLoaded = true
}
}
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
Expand Down Expand Up @@ -506,6 +532,11 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
startActivity(intentShare)


} else if (requestCode == REQUEST_CODE_CAMERA) {
if (!activitiesLoaded) {
Activities.load(this)
activitiesLoaded = true
}
}
}

Expand Down Expand Up @@ -584,7 +615,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
if (useCredentials)
(application as ProofModeApp).initContentCredentials()

startActivity(intentCam)
startActivityForResult(intentCam,REQUEST_CODE_CAMERA)
}


Expand All @@ -594,6 +625,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
private const val REQUEST_CODE_REQUIRED_PERMISSIONS = 9998
private const val REQUEST_CODE_OPTIONAL_PERMISSIONS = 9997
private const val REQUEST_CODE_CHOOSE_MEDIA = 9996
private const val REQUEST_CODE_CAMERA = 9995

/**
* The permissions needed for "base" ProofMode to work, without extra options.
Expand All @@ -604,6 +636,9 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
private val optionalPermissions = arrayOf(
Manifest.permission.ACCESS_NETWORK_STATE,
)

var activitiesLoaded = false

}

override fun openCamera() {
Expand Down

0 comments on commit e852d7f

Please sign in to comment.