Skip to content

Commit

Permalink
v2.1, Added ability to open file from file manager using intent #4 #6
Browse files Browse the repository at this point in the history
  • Loading branch information
WirelessAlien committed Oct 18, 2023
1 parent db66326 commit 9d80d3b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
1 change: 1 addition & 0 deletions .idea/.name

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

5 changes: 2 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "com.wirelessalien.zipxtract"
minSdk 23
targetSdk 33
versionCode 3
versionName "2.1"
versionCode 4
versionName "2.2"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down Expand Up @@ -51,6 +51,5 @@ dependencies {

implementation 'org.apache.commons:commons-compress:1.23.0'
implementation 'org.tukaani:xz:1.9'


}
8 changes: 8 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter
android:label="Extract files"
tools:ignore="AppLinkUrlError">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*"/>

</intent-filter>

<meta-data
android:name="android.app.lib_name"
Expand Down
36 changes: 27 additions & 9 deletions app/src/main/java/com/wirelessalien/zipxtract/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.os.Build
import android.os.Bundle
import android.provider.DocumentsContract
import android.provider.MediaStore
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.ProgressBar
Expand Down Expand Up @@ -102,6 +103,18 @@ class MainActivity : AppCompatActivity() {
}
}

if (intent?.action == Intent.ACTION_VIEW) {
val uri = intent.data
Log.d("IntentData", "URI: $uri")
if (uri != null) {
archiveFileUri = uri
extractButton.isEnabled = true
} else {
showToast("No file selected")
}
}



val savedDirectoryUri = sharedPreferences.getString("outputDirectoryUri", null)
if (savedDirectoryUri != null) {
Expand Down Expand Up @@ -221,17 +234,14 @@ class MainActivity : AppCompatActivity() {
}
} catch (e: Exception) {
showToast("Extraction failed: ${e.message}")
zipInputStream.closeEntry()
zipInputStream.close()
return
}
}
}
}
zipInputStream.closeEntry()
entry = zipInputStream.nextEntry
}

zipInputStream.close()
showExtractionCompletedSnackbar(outputDirectory)
}
Expand Down Expand Up @@ -349,7 +359,6 @@ class MainActivity : AppCompatActivity() {
outputStream.write(buffer, 0, count)
}
} catch (e: Exception) {
showToast("Extraction failed: ${e.message}")
sevenZFile.close()
}
}
Expand Down Expand Up @@ -416,18 +425,27 @@ class MainActivity : AppCompatActivity() {


private fun getArchiveFileName(archiveFileUri: Uri?): String? {
val projection = arrayOf(MediaStore.MediaColumns.DISPLAY_NAME)
val cursor = contentResolver.query(archiveFileUri!!, projection, null, null, null)
val cursor = contentResolver.query(archiveFileUri!!, null, null, null, null)
cursor?.use {
if (it.moveToFirst()) {
val nameIndex = it.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME)
return it.getString(nameIndex)
// Try to get the display name from the cursor, if not, use the last segment of the URI.
val displayNameIndex = it.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME)
if (displayNameIndex != -1) {
return it.getString(displayNameIndex)
} else {
val path = archiveFileUri.path
if (path != null) {
val slashIndex = path.lastIndexOf('/')
if (slashIndex >= 0 && slashIndex < path.length - 1) {
return path.substring(slashIndex + 1)
}
}
}
}
}
return null
}


private fun showToast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
Expand Down
6 changes: 2 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
android:layout_height="wrap_content"
android:textSize="35sp"
android:text="@string/app_name"
android:textAlignment="center"
android:layout_margin="90dp"
android:layout_marginVertical="90dp"
android:textAllCaps="true"
android:textStyle="bold"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp" />
android:layout_centerHorizontal="true"/>

<com.google.android.material.button.MaterialButton
android:id="@+id/pickFileButton"
Expand Down

0 comments on commit 9d80d3b

Please sign in to comment.