Skip to content

Commit

Permalink
Add Google Java Format via Spotless (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton authored Mar 11, 2024
1 parent ce1149e commit 7702105
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 32 deletions.
17 changes: 14 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ buildscript {
gradlePluginPortal()
}
dependencies {
classpath 'com.android.tools.build:gradle:8.3.0'
classpath 'com.vanniktech:gradle-maven-publish-plugin:0.27.0'
classpath libs.androidPlugin
classpath libs.gradleMavenPublishPlugin
classpath libs.spotlessPlugin
}
}

allprojects {
subprojects {
repositories {
mavenCentral()
google()
Expand All @@ -24,6 +25,16 @@ allprojects {
targetCompatibility JavaVersion.VERSION_1_7
}
}

apply plugin: 'com.diffplug.spotless'
spotless {
java {
googleJavaFormat(libs.googleJavaFormat.get().version)
.formatJavadoc(false)
removeUnusedImports()
target 'src/**/*.java'
}
}
}

ext {
Expand Down
6 changes: 6 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[libraries]
androidPlugin = "com.android.tools.build:gradle:8.3.0"
gradleMavenPublishPlugin = "com.vanniktech:gradle-maven-publish-plugin:0.27.0"
spotlessPlugin = "com.diffplug.spotless:spotless-plugin-gradle:6.25.0"

googleJavaFormat = "com.google.googlejavaformat:google-java-format:1.21.0"
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
package com.jakewharton.processphoenix;

import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static android.content.pm.PackageManager.FEATURE_LEANBACK;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
Expand All @@ -28,10 +32,6 @@
import java.util.Arrays;
import java.util.List;

import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static android.content.pm.PackageManager.FEATURE_LEANBACK;

/**
* Process Phoenix facilitates restarting your application process. This should only be used for
* things like fundamental state changes in your debug builds (e.g., changing from staging to
Expand Down Expand Up @@ -67,7 +67,8 @@ public static void triggerRebirth(Context context, Intent... nextIntents) {

Intent intent = new Intent(context, ProcessPhoenix.class);
intent.addFlags(FLAG_ACTIVITY_NEW_TASK); // In case we are called with non-Activity context.
intent.putParcelableArrayListExtra(KEY_RESTART_INTENTS, new ArrayList<>(Arrays.asList(nextIntents)));
intent.putParcelableArrayListExtra(
KEY_RESTART_INTENTS, new ArrayList<>(Arrays.asList(nextIntents)));
intent.putExtra(KEY_MAIN_PROCESS_PID, Process.myPid());
context.startActivity(intent);
}
Expand All @@ -88,19 +89,21 @@ private static Intent getRestartIntent(Context context) {
return defaultIntent;
}

throw new IllegalStateException("Unable to determine default activity for "
+ packageName
+ ". Does an activity specify the DEFAULT category in its intent filter?");
throw new IllegalStateException(
"Unable to determine default activity for "
+ packageName
+ ". Does an activity specify the DEFAULT category in its intent filter?");
}

@Override protected void onCreate(Bundle savedInstanceState) {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Process.killProcess(getIntent().getIntExtra(KEY_MAIN_PROCESS_PID, -1)); // Kill original main process
Process.killProcess(
getIntent().getIntExtra(KEY_MAIN_PROCESS_PID, -1)); // Kill original main process

Intent[] intents = getIntent()
.<Intent>getParcelableArrayListExtra(KEY_RESTART_INTENTS)
.toArray(new Intent[0]);
Intent[] intents =
getIntent().<Intent>getParcelableArrayListExtra(KEY_RESTART_INTENTS).toArray(new Intent[0]);

if (Build.VERSION.SDK_INT > 31) {
// Disable strict mode complaining about out-of-process intents. Normally you save and restore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
public final class MainActivity extends Activity {
private static final String EXTRA_TEXT = "text";

@Override protected void onCreate(Bundle savedInstanceState) {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
Expand All @@ -24,20 +25,22 @@ public final class MainActivity extends Activity {
processIdView.setText("Process ID: " + Process.myPid());
extraTextView.setText("Extra Text: " + getIntent().getStringExtra(EXTRA_TEXT));

restartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ProcessPhoenix.triggerRebirth(MainActivity.this);
}
});

restartWithIntentButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent nextIntent = new Intent(MainActivity.this, MainActivity.class);
nextIntent.putExtra(EXTRA_TEXT, "Hello!");
ProcessPhoenix.triggerRebirth(MainActivity.this, nextIntent);
}
});
restartButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
ProcessPhoenix.triggerRebirth(MainActivity.this);
}
});

restartWithIntentButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent nextIntent = new Intent(MainActivity.this, MainActivity.class);
nextIntent.putExtra(EXTRA_TEXT, "Hello!");
ProcessPhoenix.triggerRebirth(MainActivity.this, nextIntent);
}
});
}
}

0 comments on commit 7702105

Please sign in to comment.