Skip to content

Commit 23eff21

Browse files
Add activity and fragment.
1 parent 19ed83c commit 23eff21

File tree

14 files changed

+339
-16
lines changed

14 files changed

+339
-16
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,20 @@
99
android:roundIcon="@mipmap/ic_launcher_round"
1010
android:supportsRtl="true"
1111
android:theme="@style/AppTheme">
12-
<activity android:name=".MainActivity">
12+
13+
<activity android:name=".activity.MainActivity"
14+
android:launchMode="singleTop"
15+
android:screenOrientation="portrait" >
1316
<intent-filter>
1417
<action android:name="android.intent.action.MAIN" />
15-
1618
<category android:name="android.intent.category.LAUNCHER" />
1719
</intent-filter>
1820
</activity>
21+
22+
<activity android:name=".activity.CallActivity"
23+
android:launchMode="singleTop"
24+
android:screenOrientation="portrait"
25+
android:exported="false" />
1926
</application>
2027

2128
</manifest>

app/src/main/java/jp/co/optim/techblog_android_notification_sample/MainActivity.kt

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package jp.co.optim.techblog_android_notification_sample.activity
2+
3+
import android.os.Bundle
4+
import androidx.appcompat.app.AppCompatActivity
5+
import androidx.fragment.app.Fragment
6+
import jp.co.optim.techblog_android_notification_sample.R
7+
import jp.co.optim.techblog_android_notification_sample.extension.logD
8+
import jp.co.optim.techblog_android_notification_sample.extension.logI
9+
import jp.co.optim.techblog_android_notification_sample.extension.tag
10+
import jp.co.optim.techblog_android_notification_sample.fragment.CallingFragment
11+
import jp.co.optim.techblog_android_notification_sample.fragment.TalkingFragment
12+
13+
class CallActivity : AppCompatActivity(), CallingFragment.Callback, TalkingFragment.Callback {
14+
15+
companion object {
16+
const val CALL_ACCEPTED = "call_accepted"
17+
}
18+
19+
private val fragmentManager = supportFragmentManager
20+
21+
override fun onCreate(savedInstanceState: Bundle?) {
22+
super.onCreate(savedInstanceState)
23+
setContentView(R.layout.activity_call)
24+
25+
val callAccepted = intent.getBooleanExtra(CALL_ACCEPTED, false)
26+
logI("Is call accepted: $callAccepted")
27+
replaceFragment(if (callAccepted) TalkingFragment() else CallingFragment())
28+
}
29+
30+
override fun onCalledAccept() {
31+
logD("onCalledAccept()")
32+
replaceFragment(TalkingFragment())
33+
}
34+
35+
override fun onCalledRefuse() {
36+
logD("onCalledRefuse()")
37+
finish()
38+
}
39+
40+
override fun onCalledEnd() {
41+
logD("onCalledEnd()")
42+
finish()
43+
}
44+
45+
private fun replaceFragment(fragment: Fragment) {
46+
logI("Replace fragment: ${fragment.tag()}")
47+
fragmentManager.beginTransaction().apply {
48+
replace(R.id.fragment_container, fragment)
49+
}.commit()
50+
}
51+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package jp.co.optim.techblog_android_notification_sample.activity
2+
3+
import android.content.Intent
4+
import android.os.Bundle
5+
import androidx.appcompat.app.AppCompatActivity
6+
import jp.co.optim.techblog_android_notification_sample.R
7+
import jp.co.optim.techblog_android_notification_sample.extension.logI
8+
import jp.co.optim.techblog_android_notification_sample.extension.tag
9+
import kotlinx.android.synthetic.main.activity_main.*
10+
11+
class MainActivity : AppCompatActivity() {
12+
13+
override fun onCreate(savedInstanceState: Bundle?) {
14+
super.onCreate(savedInstanceState)
15+
setContentView(R.layout.activity_main)
16+
17+
textView_main.text = tag()
18+
button_showCalling.setOnClickListener {
19+
logI("Clicked showCalling.")
20+
startActivity(Intent(this, CallActivity::class.java))
21+
}
22+
button_postNotification.setOnClickListener {
23+
logI("Clicked postNotification.")
24+
}
25+
button_postCallNotification.setOnClickListener {
26+
logI("Clicked postCallNotification.")
27+
}
28+
}
29+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package jp.co.optim.techblog_android_notification_sample.extension
2+
3+
import android.util.Log
4+
5+
fun Any.tag(): String = this::class.java.simpleName
6+
7+
fun Any.logV(message: String) = Log.v(tag(), message)
8+
9+
fun Any.logD(message: String) = Log.d(tag(), message)
10+
11+
fun Any.logI(message: String) = Log.i(tag(), message)
12+
13+
fun Any.logW(message: String) = Log.w(tag(), message)
14+
15+
fun Any.logE(message: String) = Log.e(tag(), message)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package jp.co.optim.techblog_android_notification_sample.fragment
2+
3+
import android.os.Bundle
4+
import android.view.LayoutInflater
5+
import android.view.View
6+
import android.view.ViewGroup
7+
import androidx.fragment.app.Fragment
8+
import jp.co.optim.techblog_android_notification_sample.R
9+
import jp.co.optim.techblog_android_notification_sample.extension.tag
10+
import kotlinx.android.synthetic.main.fragment_calling.*
11+
12+
class CallingFragment : Fragment() {
13+
14+
interface Callback {
15+
fun onCalledAccept()
16+
fun onCalledRefuse()
17+
}
18+
19+
override fun onCreateView(
20+
inflater: LayoutInflater,
21+
container: ViewGroup?,
22+
savedInstanceState: Bundle?
23+
): View? = inflater.inflate(R.layout.fragment_calling, container, false)
24+
25+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
26+
super.onViewCreated(view, savedInstanceState)
27+
28+
textView_calling.text = tag()
29+
button_accept.setOnClickListener {
30+
getCallback()?.onCalledAccept()
31+
}
32+
button_refuse.setOnClickListener {
33+
getCallback()?.onCalledRefuse()
34+
}
35+
}
36+
37+
private fun getCallback(): Callback? {
38+
val callbackActivity = activity
39+
return if (callbackActivity is Callback) callbackActivity else null
40+
}
41+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package jp.co.optim.techblog_android_notification_sample.fragment
2+
3+
import android.os.Bundle
4+
import android.view.LayoutInflater
5+
import android.view.View
6+
import android.view.ViewGroup
7+
import androidx.fragment.app.Fragment
8+
import jp.co.optim.techblog_android_notification_sample.R
9+
import jp.co.optim.techblog_android_notification_sample.extension.tag
10+
import kotlinx.android.synthetic.main.fragment_talking.*
11+
12+
class TalkingFragment : Fragment() {
13+
14+
interface Callback {
15+
fun onCalledEnd()
16+
}
17+
18+
override fun onCreateView(
19+
inflater: LayoutInflater,
20+
container: ViewGroup?,
21+
savedInstanceState: Bundle?
22+
): View? = inflater.inflate(R.layout.fragment_talking, container, false)
23+
24+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
25+
super.onViewCreated(view, savedInstanceState)
26+
27+
textView_talking.text = tag()
28+
button_end.setOnClickListener {
29+
getCallback()?.onCalledEnd()
30+
}
31+
}
32+
33+
private fun getCallback(): Callback? {
34+
val callbackActivity = activity
35+
return if (callbackActivity is Callback) callbackActivity else null
36+
}
37+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
tools:context=".activity.MainActivity">
8+
9+
<androidx.constraintlayout.widget.ConstraintLayout
10+
android:id="@+id/fragment_container"
11+
android:layout_width="match_parent"
12+
android:layout_height="match_parent"
13+
app:layout_constraintBottom_toBottomOf="parent"
14+
app:layout_constraintLeft_toLeftOf="parent"
15+
app:layout_constraintRight_toRightOf="parent"
16+
app:layout_constraintTop_toTopOf="parent" />
17+
18+
</androidx.constraintlayout.widget.ConstraintLayout>

app/src/main/res/layout/activity_main.xml

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,53 @@
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
66
android:layout_height="match_parent"
7-
tools:context=".MainActivity">
7+
tools:context=".activity.MainActivity">
88

99
<TextView
10+
android:id="@+id/textView_main"
1011
android:layout_width="wrap_content"
1112
android:layout_height="wrap_content"
12-
android:text="Hello World!"
13+
android:textSize="24sp"
14+
android:textColor="@color/colorPrimaryDark"
1315
app:layout_constraintBottom_toBottomOf="parent"
1416
app:layout_constraintLeft_toLeftOf="parent"
1517
app:layout_constraintRight_toRightOf="parent"
1618
app:layout_constraintTop_toTopOf="parent" />
1719

20+
<Button
21+
android:id="@+id/button_showCalling"
22+
android:layout_width="match_parent"
23+
android:layout_height="wrap_content"
24+
android:layout_margin="30dp"
25+
android:text="@string/button_showCalling"
26+
android:textColor="@android:color/white"
27+
android:background="@color/colorAccent"
28+
app:layout_constraintStart_toStartOf="parent"
29+
app:layout_constraintEnd_toEndOf="parent"
30+
app:layout_constraintBottom_toTopOf="@id/button_postNotification"/>
31+
32+
<Button
33+
android:id="@+id/button_postNotification"
34+
android:layout_width="match_parent"
35+
android:layout_height="wrap_content"
36+
android:layout_margin="30dp"
37+
android:text="@string/button_postNotification"
38+
android:textColor="@android:color/white"
39+
android:background="@color/colorPrimary"
40+
app:layout_constraintStart_toStartOf="parent"
41+
app:layout_constraintEnd_toEndOf="parent"
42+
app:layout_constraintBottom_toTopOf="@id/button_postCallNotification"/>
43+
44+
<Button
45+
android:id="@+id/button_postCallNotification"
46+
android:layout_width="match_parent"
47+
android:layout_height="wrap_content"
48+
android:layout_margin="30dp"
49+
android:text="@string/button_postCallNotification"
50+
android:textColor="@android:color/white"
51+
android:background="@color/colorPrimaryDark"
52+
app:layout_constraintStart_toStartOf="parent"
53+
app:layout_constraintEnd_toEndOf="parent"
54+
app:layout_constraintBottom_toBottomOf="parent" />
55+
1856
</androidx.constraintlayout.widget.ConstraintLayout>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
tools:context=".activity.MainActivity">
8+
9+
<TextView
10+
android:id="@+id/textView_calling"
11+
android:layout_width="wrap_content"
12+
android:layout_height="wrap_content"
13+
android:textSize="24sp"
14+
android:textColor="@color/colorPrimaryDark"
15+
app:layout_constraintBottom_toBottomOf="parent"
16+
app:layout_constraintLeft_toLeftOf="parent"
17+
app:layout_constraintRight_toRightOf="parent"
18+
app:layout_constraintTop_toTopOf="parent" />
19+
20+
<Button
21+
android:id="@+id/button_refuse"
22+
android:layout_width="wrap_content"
23+
android:layout_height="wrap_content"
24+
android:layout_margin="30dp"
25+
android:text="@string/button_refuse"
26+
android:textColor="@android:color/white"
27+
android:background="@color/colorRefuse"
28+
app:layout_constraintBottom_toBottomOf="parent"
29+
app:layout_constraintEnd_toStartOf="@id/button_accept"
30+
app:layout_constraintStart_toStartOf="parent" />
31+
32+
<Button
33+
android:id="@+id/button_accept"
34+
android:layout_width="wrap_content"
35+
android:layout_height="wrap_content"
36+
android:layout_margin="30dp"
37+
android:text="@string/button_accept"
38+
android:textColor="@android:color/white"
39+
android:background="@color/colorAccept"
40+
app:layout_constraintStart_toEndOf="@id/button_refuse"
41+
app:layout_constraintBottom_toBottomOf="parent"
42+
app:layout_constraintEnd_toEndOf="parent" />
43+
44+
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)