Skip to content

Commit b2bf8b1

Browse files
committed
Initial setup - add new module ImageEditor
1 parent da4b484 commit b2bf8b1

File tree

20 files changed

+460
-4
lines changed

20 files changed

+460
-4
lines changed

ImageEditor/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

ImageEditor/build.gradle

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
apply plugin: 'com.android.library'
2+
apply plugin: 'kotlin-android'
3+
apply plugin: 'kotlin-android-extensions'
4+
android {
5+
compileSdkVersion 29
6+
buildToolsVersion "29.0.2"
7+
8+
9+
defaultConfig {
10+
minSdkVersion 21
11+
targetSdkVersion 29
12+
versionCode 1
13+
versionName "1.0"
14+
15+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
16+
17+
}
18+
19+
buildTypes {
20+
release {
21+
minifyEnabled false
22+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23+
}
24+
}
25+
26+
sourceSets {
27+
main.java.srcDirs += 'src/main/kotlin'
28+
release.java.srcDirs += 'src/release/kotlin'
29+
debug.java.srcDirs += 'src/debug/kotlin'
30+
test.java.srcDirs += 'src/test/kotlin'
31+
androidTest.java.srcDirs += 'src/androidTest/kotlin'
32+
}
33+
34+
}
35+
36+
dependencies {
37+
implementation fileTree(dir: 'libs', include: ['*.jar'])
38+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
39+
implementation 'androidx.appcompat:appcompat:1.0.2'
40+
implementation 'androidx.core:core-ktx:1.0.1'
41+
testImplementation 'junit:junit:4.12'
42+
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
43+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
44+
45+
implementation 'com.github.bumptech.glide:glide:4.10.0'
46+
}

ImageEditor/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.automattic.android.imageeditor
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.assertEquals
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("org.wordpress.android.imageeditor.test", appContext.packageName)
23+
}
24+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.automattic.android.imageeditor">
4+
<application>
5+
6+
<activity
7+
android:name=".EditImageActivity"
8+
android:configChanges="orientation|keyboardHidden|screenSize"
9+
android:theme="@style/Theme.AppCompat.NoActionBar">
10+
</activity>
11+
</application>
12+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.automattic.android.imageeditor
2+
3+
import android.content.Context
4+
import android.content.Intent
5+
import android.graphics.drawable.ColorDrawable
6+
import android.os.Bundle
7+
import android.os.Handler
8+
import android.text.TextUtils
9+
import android.view.MenuItem
10+
import android.view.View
11+
import androidx.appcompat.app.AppCompatActivity
12+
import androidx.appcompat.widget.Toolbar
13+
import androidx.core.app.ActivityCompat
14+
import androidx.core.app.ActivityOptionsCompat
15+
import androidx.core.content.ContextCompat
16+
import androidx.core.view.ViewCompat
17+
import androidx.fragment.app.FragmentTransaction
18+
19+
import com.automattic.android.imageeditor.fragments.MainImageFragment
20+
21+
class EditImageActivity : AppCompatActivity() {
22+
// initial media item to show, based either on ID or URI
23+
private var mediaId: Int = 0
24+
private var contentUri: String? = null
25+
private var toolbar: Toolbar? = null
26+
27+
override fun onCreate(savedInstanceState: Bundle?) {
28+
super.onCreate(savedInstanceState)
29+
30+
setContentView(R.layout.edit_image_activity)
31+
32+
if (savedInstanceState != null) {
33+
mediaId = savedInstanceState.getInt(MainImageFragment.ARG_MEDIA_ID)
34+
contentUri = savedInstanceState.getString(MainImageFragment.ARG_MEDIA_CONTENT_URI)
35+
} else {
36+
mediaId = intent.getIntExtra(MainImageFragment.ARG_MEDIA_ID, 0)
37+
contentUri = intent.getStringExtra(MainImageFragment.ARG_MEDIA_CONTENT_URI)
38+
}
39+
40+
if (TextUtils.isEmpty(contentUri)) {
41+
delayedFinish()
42+
return
43+
}
44+
45+
toolbar = findViewById(R.id.toolbar)
46+
47+
toolbar?.let {
48+
val toolbarColor = ContextCompat.getColor(this,
49+
R.color.black_translucent_40
50+
)
51+
val drawable = ColorDrawable(toolbarColor)
52+
ViewCompat.setBackground(it, drawable)
53+
}
54+
55+
setSupportActionBar(toolbar)
56+
57+
val actionBar = supportActionBar
58+
if (actionBar != null) {
59+
actionBar.setDisplayShowTitleEnabled(false)
60+
actionBar.setDisplayHomeAsUpEnabled(true)
61+
}
62+
63+
val fragmentContainer = findViewById<View>(R.id.fragment_container)
64+
fragmentContainer.visibility = View.VISIBLE
65+
showMainImageFragment()
66+
}
67+
68+
override fun onOptionsItemSelected(item: MenuItem): Boolean {
69+
if (item.itemId == android.R.id.home) {
70+
onBackPressed()
71+
return true
72+
}
73+
return super.onOptionsItemSelected(item)
74+
}
75+
76+
override fun onSaveInstanceState(outState: Bundle) {
77+
super.onSaveInstanceState(outState)
78+
outState.putInt(MainImageFragment.ARG_MEDIA_ID, mediaId)
79+
outState.putString(MainImageFragment.ARG_MEDIA_CONTENT_URI, contentUri)
80+
}
81+
82+
private fun delayedFinish() {
83+
Handler().postDelayed({ finish() }, 1500)
84+
}
85+
86+
private fun showMainImageFragment() {
87+
contentUri?.let {
88+
val fragment = MainImageFragment.newInstance(it)
89+
90+
supportFragmentManager.beginTransaction()
91+
.replace(R.id.fragment_container, fragment, MainImageFragment.TAG)
92+
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
93+
.commit()
94+
}
95+
}
96+
97+
companion object {
98+
fun startIntent(context: Context, intent: Intent) {
99+
val options = ActivityOptionsCompat.makeCustomAnimation(
100+
context,
101+
R.anim.fade_in,
102+
R.anim.fade_out
103+
)
104+
ActivityCompat.startActivity(context, intent, options.toBundle())
105+
}
106+
}
107+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
package com.automattic.android.imageeditor
3+
4+
import android.content.Context
5+
import android.content.Intent
6+
import com.automattic.android.imageeditor.fragments.MainImageFragment
7+
8+
class ImageEditor {
9+
var imageUrls: ArrayList<String> = ArrayList()
10+
11+
val isSingleImageAndCapability: Boolean
12+
get() = imageUrls.size == 1
13+
14+
/**
15+
* @param context self explanatory
16+
* @param contentUri URI of initial media - can be local or remote
17+
*/
18+
fun edit(
19+
context: Context,
20+
contentUri: String
21+
) {
22+
// TODO
23+
// Temporarily goes to edit image activity
24+
val intent = Intent(context, EditImageActivity::class.java)
25+
intent.putExtra(MainImageFragment.ARG_MEDIA_CONTENT_URI, contentUri)
26+
27+
EditImageActivity.startIntent(context, intent)
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.automattic.android.imageeditor.fragments
2+
3+
import android.graphics.drawable.Drawable
4+
import android.os.Bundle
5+
import android.view.LayoutInflater
6+
import android.view.View
7+
import android.view.ViewGroup
8+
import android.widget.ImageView
9+
import android.widget.ProgressBar
10+
11+
import androidx.fragment.app.Fragment
12+
import com.automattic.android.imageeditor.R
13+
14+
import com.bumptech.glide.Glide
15+
import com.bumptech.glide.load.DataSource
16+
17+
import com.bumptech.glide.load.engine.GlideException
18+
import com.bumptech.glide.request.RequestListener
19+
import com.bumptech.glide.request.target.Target
20+
21+
class MainImageFragment : Fragment() {
22+
private var contentUri: String? = null
23+
private var fragmentWasPaused: Boolean = false
24+
25+
private var imageView: ImageView? = null
26+
private var progressBar: ProgressBar? = null
27+
28+
override fun onCreate(savedInstanceState: Bundle?) {
29+
super.onCreate(savedInstanceState)
30+
31+
val args = arguments
32+
contentUri = args?.getString(ARG_MEDIA_CONTENT_URI)
33+
}
34+
35+
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
36+
super.onCreateView(inflater, container, savedInstanceState)
37+
38+
val view = inflater.inflate(R.layout.main_image_fragment, container, false)
39+
imageView = view.findViewById(R.id.main_image)
40+
progressBar = view.findViewById(R.id.progress_loading)
41+
42+
return view
43+
}
44+
45+
override fun onPause() {
46+
fragmentWasPaused = true
47+
48+
super.onPause()
49+
}
50+
51+
override fun onResume() {
52+
super.onResume()
53+
54+
if (fragmentWasPaused) {
55+
fragmentWasPaused = false
56+
} else {
57+
loadImage(contentUri)
58+
}
59+
}
60+
61+
/*
62+
* loads and displays a remote or local image
63+
*/
64+
private fun loadImage(mediaUri: String?) {
65+
imageView?.let {
66+
it.visibility = View.VISIBLE
67+
68+
// TODO
69+
Glide.with(this)
70+
.load(mediaUri)
71+
.listener(object : RequestListener<Drawable> {
72+
override fun onLoadFailed(
73+
e: GlideException?,
74+
model: Any?,
75+
target: Target<Drawable>?,
76+
isFirstResource: Boolean
77+
): Boolean {
78+
progressBar?.visibility = View.GONE
79+
return false
80+
}
81+
82+
override fun onResourceReady(
83+
resource: Drawable?,
84+
model: Any?,
85+
target: Target<Drawable>?,
86+
dataSource: DataSource?,
87+
isFirstResource: Boolean
88+
): Boolean {
89+
progressBar?.visibility = View.GONE
90+
return false
91+
}
92+
})
93+
.into(it)
94+
}
95+
}
96+
97+
companion object {
98+
const val TAG = "main_image_fragment"
99+
100+
const val ARG_MEDIA_CONTENT_URI = "content_uri"
101+
const val ARG_MEDIA_ID = "media_id"
102+
103+
/**
104+
* @param contentUri URI of media - can be local or remote
105+
*/
106+
fun newInstance(
107+
contentUri: String
108+
): MainImageFragment {
109+
val args = Bundle()
110+
args.putString(ARG_MEDIA_CONTENT_URI, contentUri)
111+
112+
val fragment = MainImageFragment()
113+
fragment.arguments = args
114+
return fragment
115+
}
116+
}
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<set xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:fillAfter="true" >
4+
5+
<alpha
6+
android:fromAlpha="0"
7+
android:toAlpha="1"
8+
android:duration="600" />
9+
</set>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<set xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:fillAfter="true" >
4+
5+
<alpha
6+
android:fromAlpha="1"
7+
android:toAlpha="0"
8+
android:duration="600" />
9+
</set>

0 commit comments

Comments
 (0)