-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
28 changed files
with
2,107 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
include ':videotrimmer' | ||
include ':app' | ||
rootProject.name = "VideoTrimmerApplication" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
apply plugin: 'com.android.library' | ||
apply plugin: 'kotlin-android' | ||
apply plugin: 'kotlin-android-extensions' | ||
|
||
android { | ||
compileSdkVersion 29 | ||
buildToolsVersion "29.0.3" | ||
|
||
defaultConfig { | ||
minSdkVersion 19 | ||
targetSdkVersion 29 | ||
versionCode 1 | ||
versionName "1.0" | ||
|
||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
consumerProguardFiles "consumer-rules.pro" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation fileTree(dir: "libs", include: ["*.jar"]) | ||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" | ||
implementation 'androidx.core:core-ktx:1.3.1' | ||
implementation 'androidx.appcompat:appcompat:1.2.0' | ||
testImplementation 'junit:junit:4.13' | ||
androidTestImplementation 'androidx.test.ext:junit:1.1.1' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' | ||
|
||
implementation 'com.intuit.sdp:sdp-android:1.0.6' | ||
implementation 'com.intuit.ssp:ssp-android:1.0.6' | ||
|
||
implementation 'com.googlecode.mp4parser:isoparser:1.1.22' | ||
} |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
24 changes: 24 additions & 0 deletions
24
...otrimmer/src/androidTest/java/com/ahmedbadereldin/videotrimmer/ExampleInstrumentedTest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.ahmedbadereldin.videotrimmer | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
|
||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
import org.junit.Assert.* | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* See [testing documentation](http://d.android.com/tools/testing). | ||
*/ | ||
@RunWith(AndroidJUnit4::class) | ||
class ExampleInstrumentedTest { | ||
@Test | ||
fun useAppContext() { | ||
// Context of the app under test. | ||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext | ||
assertEquals("com.ahmedbadereldin.videotrimmer.test", appContext.packageName) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.ahmedbadereldin.videotrimmer"> | ||
|
||
|
||
</manifest> |
189 changes: 189 additions & 0 deletions
189
videotrimmer/src/main/java/com/ahmedbadereldin/videotrimmer/FileUtil.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
package com.ahmedbadereldin.videotrimmer; | ||
|
||
import android.content.ContentResolver; | ||
import android.content.Context; | ||
import android.database.Cursor; | ||
import android.net.Uri; | ||
import android.provider.OpenableColumns; | ||
import android.text.TextUtils; | ||
import android.util.Log; | ||
import android.webkit.MimeTypeMap; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
public class FileUtil { | ||
private static final int EOF = -1; | ||
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; | ||
|
||
// By default, Android doesn't provide support for JSON | ||
public static final String MIME_TYPE_JSON = "application/json"; | ||
|
||
private FileUtil() { | ||
|
||
} | ||
|
||
public static File from(Context context, Uri uri) throws IOException { | ||
InputStream inputStream = context.getContentResolver().openInputStream(uri); | ||
String fileName = getFileName(context, uri); | ||
String[] splitName = splitFileName(fileName); | ||
File tempFile = File.createTempFile(splitName[0], splitName[1]); | ||
tempFile = rename(tempFile, fileName); | ||
tempFile.deleteOnExit(); | ||
FileOutputStream out = null; | ||
try { | ||
out = new FileOutputStream(tempFile); | ||
} catch (FileNotFoundException e) { | ||
e.printStackTrace(); | ||
} | ||
if (inputStream != null) { | ||
copy(inputStream, out); | ||
inputStream.close(); | ||
} | ||
|
||
if (out != null) { | ||
out.close(); | ||
} | ||
return tempFile; | ||
} | ||
|
||
private static String[] splitFileName(String fileName) { | ||
String name = fileName; | ||
String extension = ""; | ||
int i = fileName.lastIndexOf("."); | ||
if (i != -1) { | ||
name = fileName.substring(0, i); | ||
extension = fileName.substring(i); | ||
} | ||
|
||
return new String[]{name, extension}; | ||
} | ||
|
||
private static String getFileName(Context context, Uri uri) { | ||
String result = null; | ||
if (uri.getScheme().equals("content")) { | ||
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null); | ||
try { | ||
if (cursor != null && cursor.moveToFirst()) { | ||
result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} finally { | ||
if (cursor != null) { | ||
cursor.close(); | ||
} | ||
} | ||
} | ||
if (result == null) { | ||
result = uri.getPath(); | ||
int cut = result.lastIndexOf(File.separator); | ||
if (cut != -1) { | ||
result = result.substring(cut + 1); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
private static File rename(File file, String newName) { | ||
File newFile = new File(file.getParent(), newName); | ||
if (!newFile.equals(file)) { | ||
if (newFile.exists() && newFile.delete()) { | ||
Log.d("FileUtil", "Delete old " + newName + " file"); | ||
} | ||
if (file.renameTo(newFile)) { | ||
Log.d("FileUtil", "Rename file to " + newName); | ||
} | ||
} | ||
return newFile; | ||
} | ||
|
||
private static long copy(InputStream input, OutputStream output) throws IOException { | ||
long count = 0; | ||
int n; | ||
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; | ||
while (EOF != (n = input.read(buffer))) { | ||
output.write(buffer, 0, n); | ||
count += n; | ||
} | ||
return count; | ||
} | ||
|
||
@Nullable | ||
public static String getMimeType(@NonNull Context context, @NonNull Uri uri) { | ||
|
||
String mimeType = null; | ||
if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) { | ||
ContentResolver cr = context.getContentResolver(); | ||
mimeType = cr.getType(uri); | ||
} else { | ||
String fileExtension = getExtension(uri.toString()); | ||
|
||
if(fileExtension == null){ | ||
return null; | ||
} | ||
|
||
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension( | ||
fileExtension.toLowerCase()); | ||
|
||
if(mimeType == null){ | ||
// Handle the misc file extensions | ||
return handleMiscFileExtensions(fileExtension); | ||
} | ||
} | ||
return mimeType; | ||
} | ||
|
||
@Nullable | ||
private static String getExtension(@Nullable String fileName){ | ||
|
||
if(fileName == null || TextUtils.isEmpty(fileName)){ | ||
return null; | ||
} | ||
|
||
char[] arrayOfFilename = fileName.toCharArray(); | ||
for(int i = arrayOfFilename.length-1; i > 0; i--){ | ||
if(arrayOfFilename[i] == '.'){ | ||
return fileName.substring(i+1, fileName.length()); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Nullable | ||
private static String handleMiscFileExtensions(@NonNull String extension){ | ||
|
||
if(extension.equals("json")){ | ||
return MIME_TYPE_JSON; | ||
} | ||
else{ | ||
return null; | ||
} | ||
} | ||
|
||
public static String getMimeType(File file) { | ||
String mimeType = ""; | ||
String extension = getExtention(file.getName()); | ||
if (MimeTypeMap.getSingleton().hasExtension(extension)) { | ||
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); | ||
} | ||
return mimeType; | ||
} | ||
|
||
private static String getExtention(String fileName){ | ||
char[] arrayOfFilename = fileName.toCharArray(); | ||
for(int i = arrayOfFilename.length-1; i > 0; i--){ | ||
if(arrayOfFilename[i] == '.'){ | ||
return fileName.substring(i+1, fileName.length()); | ||
} | ||
} | ||
return ""; | ||
} | ||
} |
Oops, something went wrong.