Skip to content

Commit

Permalink
Merge pull request #195 from mickstar/add_barcode_scanner
Browse files Browse the repository at this point in the history
Add barcode scanner
  • Loading branch information
mickstar authored Feb 15, 2024
2 parents 68abea3 + 4c6a1cf commit b19d684
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 42 deletions.
7 changes: 5 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ android {
applicationId "com.mickstarify.zooforzotero"
minSdkVersion 23
targetSdkVersion 34
versionCode 49
versionName "3.1"
versionCode 50
versionName "3.1a"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
buildConfigField("String", "zotero_api_key", apikeyProperties['zotero_api_key'])
buildConfigField("String", "zotero_api_secret", apikeyProperties['zotero_api_secret'])
Expand Down Expand Up @@ -89,6 +89,9 @@ dependencies {

debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.7'

// barcode scanner
implementation 'com.journeyapps:zxing-android-embedded:4.3.0'

// Room Dependencies
implementation "androidx.room:room-runtime:2.6.1"
implementation 'androidx.room:room-rxjava2:2.6.1'
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@
</intent-filter>
</activity>

<activity
android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:screenOrientation="fullSensor"
tools:replace="screenOrientation" />

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.mickstarify.zooforzotero.fileprovider.app"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.ProgressDialog
import android.content.ActivityNotFoundException
import android.content.Intent
import android.content.res.Configuration
import android.graphics.Rect
import android.net.Uri
import android.os.Bundle
import android.os.Handler
Expand All @@ -12,26 +13,26 @@ import android.util.Log
import android.util.SparseArray
import android.view.Menu
import android.view.MenuItem
import android.view.WindowManager
import android.widget.Toast
import android.view.MotionEvent
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.graphics.Rect
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.core.content.getSystemService
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.core.content.getSystemService
import androidx.core.view.GravityCompat
import androidx.core.view.iterator
import androidx.drawerlayout.widget.DrawerLayout
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import com.google.android.material.navigation.NavigationView
import com.journeyapps.barcodescanner.ScanContract
import com.journeyapps.barcodescanner.ScanOptions
import com.mickstarify.zooforzotero.AttachmentManager.AttachmentManager
import com.mickstarify.zooforzotero.LibraryActivity.ItemView.ItemAttachmentEntry
import com.mickstarify.zooforzotero.LibraryActivity.ItemView.ItemViewFragment
Expand All @@ -46,6 +47,8 @@ import com.mickstarify.zooforzotero.ZoteroStorage.Database.GroupInfo
import com.mickstarify.zooforzotero.ZoteroStorage.Database.Item
import dagger.hilt.android.AndroidEntryPoint

private const val TAG = "LibraryActivity"

@AndroidEntryPoint
class LibraryActivity : AppCompatActivity(),
NavigationView.OnNavigationItemSelectedListener,
Expand Down Expand Up @@ -92,11 +95,13 @@ class LibraryActivity : AppCompatActivity(),
R.id.libraryListFragment -> {
libraryListScreenShown()
}

R.id.libraryLoadingScreen -> {
libraryLoadingScreenShown()
}

else -> {
throw(NotImplementedError("Error screen $destination not handled."))
throw (NotImplementedError("Error screen $destination not handled."))
}
}
}
Expand All @@ -109,43 +114,27 @@ class LibraryActivity : AppCompatActivity(),
}
}

private val getBarcode = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
val data: Intent? = result.data
val contents = data?.getStringExtra("SCAN_RESULT")
Log.i("zotero", "got barcode scan result $contents")
if(contents != null) {
libraryListViewModel.scannedBarcodeNumber(contents)
}
private val barcodeLauncher = registerForActivityResult(
ScanContract()
) {
if (it.contents != null) {
Log.i(TAG, "scanned barcode: ${it.contents}")
libraryListViewModel.scannedBarcodeNumber(it.contents)
} else {
Log.e(TAG, "error scanning barcode.")
}
}

private fun handleBarcodeScanRequest(){
val intent = Intent("com.google.zxing.client.android.SCAN")
intent.setPackage("com.google.zxing.client.android")
intent.putExtra("SCAN_MODE", "PRODUCT_MODE")
intent.putExtra("SCAN_FORMATS", "EAN_13")

try {
getBarcode.launch(intent)
} catch (e: ActivityNotFoundException){
Log.e("zotero", "error launching barcode scanner. ${e.message}")

val alertDialog = AlertDialog.Builder(this)
.setTitle("Barcode Scanner Not Found")
.setMessage("You need to install the ZXing barcode scanner to use this feature.")
.setPositiveButton("Install", {_, _ ->
val intent2 = Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.google.zxing.client.android"))
startActivity(intent2)
})
.setIcon(android.R.drawable.ic_dialog_alert)
.setNegativeButton("Cancel", {_, _ -> })
alertDialog.show()
}
private fun handleBarcodeScanRequest() {
barcodeLauncher.launch(ScanOptions().apply {
setDesiredBarcodeFormats(ScanOptions.EAN_13)
setPrompt("Scan a barcode")
setBarcodeImageEnabled(true)
setBeepEnabled(true)
setOrientationLocked(false)
})
}



private fun libraryLoadingScreenShown() {
supportActionBar?.title = "Loading"
mDrawerToggle.isDrawerIndicatorEnabled = true
Expand Down Expand Up @@ -294,13 +283,16 @@ class LibraryActivity : AppCompatActivity(),
val intent = Intent(this, SettingsActivity::class.java)
startActivity(intent)
}

R.id.attachment_manager -> {
val intent = Intent(this, AttachmentManager::class.java)
startActivity(intent)
}

R.id.force_resync -> {
presenter.requestForceResync()
}

android.R.id.home -> {
onBackPressed()
}
Expand Down Expand Up @@ -619,6 +611,7 @@ class LibraryActivity : AppCompatActivity(),
intent.data = Uri.parse(url)
startActivity(intent)
}

// this lets keyboard close when clicked in backgroud
override fun dispatchTouchEvent(event: MotionEvent): Boolean {
if (event.action == MotionEvent.ACTION_DOWN) {
Expand All @@ -628,7 +621,10 @@ class LibraryActivity : AppCompatActivity(),
v.getGlobalVisibleRect(outRect)
if (!outRect.contains(event.rawX.toInt(), event.rawY.toInt())) {
v.clearFocus()
val imm = getSystemService<InputMethodManager>()?.hideSoftInputFromWindow(v.windowToken, 0)
val imm = getSystemService<InputMethodManager>()?.hideSoftInputFromWindow(
v.windowToken,
0
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.mickstarify.zooforzotero

import android.app.Application
import dagger.hilt.android.HiltAndroidApp
import org.acra.ACRA
import org.acra.ReportField
import org.acra.config.dialog
import org.acra.config.mailSender
import org.acra.data.StringFormat
Expand All @@ -14,8 +16,17 @@ class ZooForZoteroApplication : Application() {

initAcra {
//core configuration:
buildConfigClass = BuildConfig::class.java
reportContent = listOf(
ReportField.APP_VERSION_CODE,
ReportField.ANDROID_VERSION,
ReportField.PHONE_MODEL,
ReportField.CUSTOM_DATA,
ReportField.STACK_TRACE,
ReportField.LOGCAT
)

reportFormat = StringFormat.JSON
excludeMatchingSharedPreferencesKeys = listOf("*")
//each plugin you chose above can be configured in a block like this:

dialog {
Expand Down

0 comments on commit b19d684

Please sign in to comment.