Skip to content

Commit

Permalink
Fix fragment observation, replace contentprovider with initializer, m…
Browse files Browse the repository at this point in the history
…odernization
  • Loading branch information
barnhill committed Apr 29, 2022
1 parent 20fbb3d commit e2f5080
Show file tree
Hide file tree
Showing 26 changed files with 174 additions and 129 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,8 @@ android {

dependencies {
debugImplementation project(":savestateobserver")
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.fragment:fragment-ktx:1.4.1'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'androidx.startup:startup-runtime:1.1.1'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.pnuema.android.savestateobserver.app

import android.content.Context
import android.util.Log
import androidx.startup.Initializer
import com.pnuema.android.savestateobserver.OversizeBundleRegistrar

class AppInitializer: Initializer<Unit> {
override fun create(context: Context) {
OversizeBundleRegistrar.register { stringifyBundle ->
Log.e(
"AppBundleWorker",
"OVERSIZE BUNDLE DETECTED: $stringifyBundle"
)
}
}

override fun dependencies(): MutableList<Class<out Initializer<*>>> = mutableListOf()

}

This file was deleted.

22 changes: 15 additions & 7 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.pnuema.android.savestateobserver.app">

<application
android:fullBackupContent="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name=".MainActivity"
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">
<meta-data
android:name="com.pnuema.android.savestateobserver.app.AppInitializer"
android:value="androidx.startup" />
</provider>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.pnuema.android.savestateobserver.app

import android.os.Bundle
import java.util.*

object BundleGenerator {
/**
* Generate oversize bundle
*/
fun Bundle.generateOversizeBundle() = apply {
putInt("Integer", 1234)
putString("String", "StringTest")
putFloat("Float", 12.34F)

val innerBundle = Bundle()
innerBundle.putInt("Integer", 5678)
innerBundle.putString("String", "InnerStringTest")
innerBundle.putFloat("Float", 56.78F)

//generate 50k of data for the bundle
var bigString = ""
while (bigString.length < 50000) {
bigString += UUID.randomUUID().toString()
}
innerBundle.putString("BigString", bigString)

putBundle("innerBundle", innerBundle)
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
package com.pnuema.android.savestateobserver.app

import android.os.Bundle
import java.util.*
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.commit
import com.pnuema.android.savestateobserver.app.BundleGenerator.generateOversizeBundle

class MainActivity : BaseActivity() {
override fun onSaveInstanceState(outState: Bundle) {
outState.putInt("Integer", 1234)
outState.putString("String", "StringTest")
outState.putFloat("Float", 12.34F)

val innerBundle = Bundle()
innerBundle.putInt("Integer", 5678)
innerBundle.putString("String", "InnerStringTest")
innerBundle.putFloat("Float", 56.78F)
class MainActivity : AppCompatActivity(R.layout.activity_main) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

//generate 50k of data for the bundle
var bigString = ""
while (bigString.length < 50000) {
bigString += UUID.randomUUID().toString()
supportFragmentManager.commit {
replace(R.id.fragment_container_view, MainFragment.newInstance())
}
innerBundle.putString("BigString", bigString)

outState.putBundle("innerBundle", innerBundle)

super.onSaveInstanceState(outState)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
override fun onSaveInstanceState(outState: Bundle) {
outState.generateOversizeBundle()
super.onSaveInstanceState(outState)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.pnuema.android.savestateobserver.app

import android.os.Bundle
import androidx.fragment.app.Fragment
import com.pnuema.android.savestateobserver.app.BundleGenerator.generateOversizeBundle

/**
* A simple [Fragment] subclass.
* Use the [MainFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class MainFragment private constructor() : Fragment(R.layout.fragment_main) {
companion object {
fun newInstance() = MainFragment().apply {
arguments = Bundle().generateOversizeBundle() //TODO detect oversize arguments
}
}

override fun onSaveInstanceState(outState: Bundle) {
outState.generateOversizeBundle()
super.onSaveInstanceState(outState)
}
}
12 changes: 3 additions & 9 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/instructions"/>

</LinearLayout>
tools:context=".MainActivity" />
17 changes: 17 additions & 0 deletions app/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainFragment">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/instructions"
android:textSize="24sp"
android:textAlignment="center"
android:layout_gravity="center"/>

</FrameLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<resources>
<string name="app_name">SaveStateObserver</string>
<string name="instructions">Minimize this app while watching the LogCat to see the results</string>
<string name="instructions">Minimize this app while watching LogCat to see the results</string>
</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.pnuema.android.savestateobserver.app

import android.content.Context
import androidx.startup.Initializer

class AppInitializer: Initializer<Unit> {
override fun create(context: Context) = Unit
override fun dependencies(): MutableList<Class<out Initializer<*>>> = mutableListOf()
}

This file was deleted.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ wrapper {
}

buildscript {
ext.kotlin_version = '1.6.20'
ext.kotlin_version = '1.6.21'

repositories {
google()
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ android.useAndroidX=true
android.enableJetifier=false

GROUP=com.pnuema.android
VERSION_NAME=2.6.1
VERSION_NAME=2.7.0

POM_NAME=SaveStateObserver
POM_ARTIFACT_ID=savestateobserver
Expand Down
3 changes: 2 additions & 1 deletion savestateobserver/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'kotlin-android'
apply plugin: 'org.jetbrains.dokka'
apply plugin: "com.vanniktech.maven.publish"

version = "2.6.1"
version = "2.7.0"
group = "com.pnuema.android"
archivesBaseName = 'savestateobserver'

Expand Down Expand Up @@ -44,6 +44,7 @@ android {

dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'androidx.startup:startup-runtime:1.1.1'
testImplementation 'junit:junit:4.13.2'
}

Expand Down
13 changes: 9 additions & 4 deletions savestateobserver/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<manifest package="com.pnuema.android.savestateobserver"
xmlns:android="http://schemas.android.com/apk/res/android">
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application>

<provider
android:authorities="${applicationId}.savestateobserverinitprovider"
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
android:enabled="true"
android:name=".SaveStateLibraryContentProvider" />
tools:node="merge">
<meta-data
android:name="com.pnuema.android.savestateobserver.SaveStateLibraryInitializer"
android:value="androidx.startup" />
</provider>

</application>

Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit e2f5080

Please sign in to comment.