From 1e6c261915be45930aff813aa9698ddb4ec9c7eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20D=C3=ADaz?= Date: Sun, 25 Sep 2016 17:49:39 -0400 Subject: [PATCH] Code refactor --- .../snorlax/module/SnorlaxComponent.java | 9 +++- .../snorlax/module/SnorlaxModule.java | 13 ----- .../module/context/pokemongo/PokemonGo.java | 28 ++++++++++ .../context/pokemongo/PokemonGoContext.java | 33 ++++++++++++ .../pokemongo/PokemonGoContextFactory.java | 32 ++++++++++++ .../pokemongo/PokemonGoContextModule.java | 51 +++++++++++++++++++ .../module/context/snorlax/Snorlax.java | 28 ++++++++++ .../context/snorlax/SnorlaxContext.java | 33 ++++++++++++ .../snorlax/SnorlaxContextFactory.java} | 21 +++----- .../context/snorlax/SnorlaxContextModule.java | 51 +++++++++++++++++++ .../feature/capture/CaptureNotification.java | 12 +++-- .../feature/capture/CapturePreferences.java | 6 +-- .../module/feature/encounter/Encounter.java | 2 + .../encounter/EncounterNotification.java | 9 ++-- .../encounter/EncounterPreferences.java | 6 +-- .../snorlax/module/feature/mitm/Mitm.java | 2 + .../module/feature/mock/MockPreferences.java | 6 +-- 17 files changed, 295 insertions(+), 47 deletions(-) create mode 100644 app/src/main/java/com/icecream/snorlax/module/context/pokemongo/PokemonGo.java create mode 100644 app/src/main/java/com/icecream/snorlax/module/context/pokemongo/PokemonGoContext.java create mode 100644 app/src/main/java/com/icecream/snorlax/module/context/pokemongo/PokemonGoContextFactory.java create mode 100644 app/src/main/java/com/icecream/snorlax/module/context/pokemongo/PokemonGoContextModule.java create mode 100644 app/src/main/java/com/icecream/snorlax/module/context/snorlax/Snorlax.java create mode 100644 app/src/main/java/com/icecream/snorlax/module/context/snorlax/SnorlaxContext.java rename app/src/main/java/com/icecream/snorlax/module/{SnorlaxContext.java => context/snorlax/SnorlaxContextFactory.java} (69%) create mode 100644 app/src/main/java/com/icecream/snorlax/module/context/snorlax/SnorlaxContextModule.java diff --git a/app/src/main/java/com/icecream/snorlax/module/SnorlaxComponent.java b/app/src/main/java/com/icecream/snorlax/module/SnorlaxComponent.java index 43cb769..d596023 100644 --- a/app/src/main/java/com/icecream/snorlax/module/SnorlaxComponent.java +++ b/app/src/main/java/com/icecream/snorlax/module/SnorlaxComponent.java @@ -18,10 +18,17 @@ import javax.inject.Singleton; +import com.icecream.snorlax.module.context.pokemongo.PokemonGoContextModule; +import com.icecream.snorlax.module.context.snorlax.SnorlaxContextModule; + import dagger.Component; @Singleton -@Component(modules = {SnorlaxModule.class}) +@Component(modules = { + SnorlaxModule.class, + SnorlaxContextModule.class, + PokemonGoContextModule.class +}) interface SnorlaxComponent { void inject(Snorlax snorlax); diff --git a/app/src/main/java/com/icecream/snorlax/module/SnorlaxModule.java b/app/src/main/java/com/icecream/snorlax/module/SnorlaxModule.java index 88b5454..60225ac 100644 --- a/app/src/main/java/com/icecream/snorlax/module/SnorlaxModule.java +++ b/app/src/main/java/com/icecream/snorlax/module/SnorlaxModule.java @@ -19,8 +19,6 @@ import javax.inject.Singleton; import android.app.Application; -import android.content.Context; -import android.content.pm.PackageManager; import com.icecream.snorlax.module.feature.mitm.MitmRelay; @@ -56,17 +54,6 @@ Application provideAppliction() { return mApplication; } - @Provides - @Singleton - Context provideContext(Application application) { - try { - return SnorlaxContext.create(application); - } - catch (PackageManager.NameNotFoundException exception) { - throw new RuntimeException("Snorlax package not found... Cannot continue"); - } - } - @Provides @Singleton MitmRelay provideMitmRelay() { diff --git a/app/src/main/java/com/icecream/snorlax/module/context/pokemongo/PokemonGo.java b/app/src/main/java/com/icecream/snorlax/module/context/pokemongo/PokemonGo.java new file mode 100644 index 0000000..1188487 --- /dev/null +++ b/app/src/main/java/com/icecream/snorlax/module/context/pokemongo/PokemonGo.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2016. Pedro Diaz + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.icecream.snorlax.module.context.pokemongo; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import javax.inject.Qualifier; + +@Qualifier +@Retention(RetentionPolicy.RUNTIME) +public @interface PokemonGo { + +} diff --git a/app/src/main/java/com/icecream/snorlax/module/context/pokemongo/PokemonGoContext.java b/app/src/main/java/com/icecream/snorlax/module/context/pokemongo/PokemonGoContext.java new file mode 100644 index 0000000..8ed607c --- /dev/null +++ b/app/src/main/java/com/icecream/snorlax/module/context/pokemongo/PokemonGoContext.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2016. Pedro Diaz + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.icecream.snorlax.module.context.pokemongo; + +import android.content.Context; +import android.content.ContextWrapper; + +@SuppressWarnings({"unused", "FieldCanBeLocal", "WeakerAccess"}) +final class PokemonGoContext extends ContextWrapper { + + PokemonGoContext(Context base) { + super(base); + } + + @Override + public Context getApplicationContext() { + return this; + } +} diff --git a/app/src/main/java/com/icecream/snorlax/module/context/pokemongo/PokemonGoContextFactory.java b/app/src/main/java/com/icecream/snorlax/module/context/pokemongo/PokemonGoContextFactory.java new file mode 100644 index 0000000..12823bf --- /dev/null +++ b/app/src/main/java/com/icecream/snorlax/module/context/pokemongo/PokemonGoContextFactory.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2016. Pedro Diaz + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.icecream.snorlax.module.context.pokemongo; + +import android.content.Context; +import android.content.pm.PackageManager; + +@SuppressWarnings({"unused", "FieldCanBeLocal", "WeakerAccess"}) +final class PokemonGoContextFactory { + + static PokemonGoContext create(Context from) throws PackageManager.NameNotFoundException { + return new PokemonGoContext(from); + } + + private PokemonGoContextFactory() { + throw new AssertionError("No instances"); + } +} diff --git a/app/src/main/java/com/icecream/snorlax/module/context/pokemongo/PokemonGoContextModule.java b/app/src/main/java/com/icecream/snorlax/module/context/pokemongo/PokemonGoContextModule.java new file mode 100644 index 0000000..2e06035 --- /dev/null +++ b/app/src/main/java/com/icecream/snorlax/module/context/pokemongo/PokemonGoContextModule.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2016. Pedro Diaz + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.icecream.snorlax.module.context.pokemongo; + +import javax.inject.Singleton; + +import android.app.Application; +import android.app.NotificationManager; +import android.content.Context; +import android.content.pm.PackageManager; + +import dagger.Module; +import dagger.Provides; + +@Module +@SuppressWarnings({"unused", "FieldCanBeLocal", "WeakerAccess"}) +public final class PokemonGoContextModule { + + @Provides + @PokemonGo + @Singleton + static Context provideContext(Application application) { + try { + return PokemonGoContextFactory.create(application); + } + catch (PackageManager.NameNotFoundException e) { + throw new RuntimeException("Pokemon Go package not found... Cannot continue"); + } + } + + @Provides + @PokemonGo + @Singleton + static NotificationManager provideNotificationManager(@PokemonGo Context context) { + return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + } +} diff --git a/app/src/main/java/com/icecream/snorlax/module/context/snorlax/Snorlax.java b/app/src/main/java/com/icecream/snorlax/module/context/snorlax/Snorlax.java new file mode 100644 index 0000000..6948ffa --- /dev/null +++ b/app/src/main/java/com/icecream/snorlax/module/context/snorlax/Snorlax.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2016. Pedro Diaz + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.icecream.snorlax.module.context.snorlax; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import javax.inject.Qualifier; + +@Qualifier +@Retention(RetentionPolicy.RUNTIME) +public @interface Snorlax { + +} diff --git a/app/src/main/java/com/icecream/snorlax/module/context/snorlax/SnorlaxContext.java b/app/src/main/java/com/icecream/snorlax/module/context/snorlax/SnorlaxContext.java new file mode 100644 index 0000000..c9e13f5 --- /dev/null +++ b/app/src/main/java/com/icecream/snorlax/module/context/snorlax/SnorlaxContext.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2016. Pedro Diaz + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.icecream.snorlax.module.context.snorlax; + +import android.content.Context; +import android.content.ContextWrapper; + +@SuppressWarnings({"unused", "FieldCanBeLocal", "WeakerAccess"}) +final class SnorlaxContext extends ContextWrapper { + + SnorlaxContext(Context base) { + super(base); + } + + @Override + public Context getApplicationContext() { + return this; + } +} diff --git a/app/src/main/java/com/icecream/snorlax/module/SnorlaxContext.java b/app/src/main/java/com/icecream/snorlax/module/context/snorlax/SnorlaxContextFactory.java similarity index 69% rename from app/src/main/java/com/icecream/snorlax/module/SnorlaxContext.java rename to app/src/main/java/com/icecream/snorlax/module/context/snorlax/SnorlaxContextFactory.java index cf7d04b..442f9b1 100644 --- a/app/src/main/java/com/icecream/snorlax/module/SnorlaxContext.java +++ b/app/src/main/java/com/icecream/snorlax/module/context/snorlax/SnorlaxContextFactory.java @@ -14,30 +14,21 @@ * limitations under the License. */ -package com.icecream.snorlax.module; +package com.icecream.snorlax.module.context.snorlax; import android.content.Context; -import android.content.ContextWrapper; import android.content.pm.PackageManager; import com.icecream.snorlax.BuildConfig; -@SuppressWarnings({"unused", "FieldCanBeLocal"}) -final class SnorlaxContext extends ContextWrapper { +@SuppressWarnings({"unused", "FieldCanBeLocal", "WeakerAccess"}) +final class SnorlaxContextFactory { static SnorlaxContext create(Context from) throws PackageManager.NameNotFoundException { - return new SnorlaxContext(from.createPackageContext(BuildConfig.SNORLAX_ID, Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE), from); + return new SnorlaxContext(from.createPackageContext(BuildConfig.SNORLAX_ID, Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE)); } - private final Context mPokemonGoContext; - - private SnorlaxContext(Context snorlax, Context pokemonGo) { - super(snorlax); - mPokemonGoContext = pokemonGo; - } - - @Override - public Context getApplicationContext() { - return this; + private SnorlaxContextFactory() { + throw new AssertionError("No instances"); } } diff --git a/app/src/main/java/com/icecream/snorlax/module/context/snorlax/SnorlaxContextModule.java b/app/src/main/java/com/icecream/snorlax/module/context/snorlax/SnorlaxContextModule.java new file mode 100644 index 0000000..2174d8c --- /dev/null +++ b/app/src/main/java/com/icecream/snorlax/module/context/snorlax/SnorlaxContextModule.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2016. Pedro Diaz + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.icecream.snorlax.module.context.snorlax; + +import javax.inject.Singleton; + +import android.app.Application; +import android.content.Context; +import android.content.pm.PackageManager; +import android.content.res.Resources; + +import dagger.Module; +import dagger.Provides; + +@Module +@SuppressWarnings({"unused", "FieldCanBeLocal", "WeakerAccess"}) +public final class SnorlaxContextModule { + + @Provides + @Snorlax + @Singleton + static Context provideContext(Application application) { + try { + return SnorlaxContextFactory.create(application); + } + catch (PackageManager.NameNotFoundException e) { + throw new RuntimeException("Snorlax package not found... Cannot continue"); + } + } + + @Provides + @Snorlax + @Singleton + static Resources provideResources(@Snorlax Context context) { + return context.getResources(); + } +} diff --git a/app/src/main/java/com/icecream/snorlax/module/feature/capture/CaptureNotification.java b/app/src/main/java/com/icecream/snorlax/module/feature/capture/CaptureNotification.java index d659854..4545478 100644 --- a/app/src/main/java/com/icecream/snorlax/module/feature/capture/CaptureNotification.java +++ b/app/src/main/java/com/icecream/snorlax/module/feature/capture/CaptureNotification.java @@ -19,22 +19,24 @@ import javax.inject.Inject; import javax.inject.Singleton; -import android.app.Application; +import android.content.Context; import android.os.Handler; import android.os.Looper; import android.widget.Toast; +import com.icecream.snorlax.module.context.pokemongo.PokemonGo; + @Singleton final class CaptureNotification { - private final Application mApplication; + private final Context mContext; @Inject - CaptureNotification(Application application) { - mApplication = application; + CaptureNotification(@PokemonGo Context context) { + mContext = context; } void show(final String message) { - new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(mApplication, message, Toast.LENGTH_SHORT).show()); + new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show()); } } diff --git a/app/src/main/java/com/icecream/snorlax/module/feature/capture/CapturePreferences.java b/app/src/main/java/com/icecream/snorlax/module/feature/capture/CapturePreferences.java index 76de9fa..dd2ee32 100644 --- a/app/src/main/java/com/icecream/snorlax/module/feature/capture/CapturePreferences.java +++ b/app/src/main/java/com/icecream/snorlax/module/feature/capture/CapturePreferences.java @@ -19,10 +19,10 @@ import javax.inject.Inject; import javax.inject.Singleton; -import android.content.Context; import android.content.res.Resources; import com.icecream.snorlax.R; +import com.icecream.snorlax.module.context.snorlax.Snorlax; import de.robv.android.xposed.XSharedPreferences; import rx.Observable; @@ -34,8 +34,8 @@ final class CapturePreferences { private final XSharedPreferences mPreferences; @Inject - CapturePreferences(Context context, XSharedPreferences preferences) { - mResources = context.getResources(); + CapturePreferences(@Snorlax Resources resources, XSharedPreferences preferences) { + mResources = resources; mPreferences = preferences; } diff --git a/app/src/main/java/com/icecream/snorlax/module/feature/encounter/Encounter.java b/app/src/main/java/com/icecream/snorlax/module/feature/encounter/Encounter.java index bd76c90..3fce3fb 100644 --- a/app/src/main/java/com/icecream/snorlax/module/feature/encounter/Encounter.java +++ b/app/src/main/java/com/icecream/snorlax/module/feature/encounter/Encounter.java @@ -70,7 +70,9 @@ private String formatMove(String move) { } private void onEncounter(PokemonData data, CaptureProbability probability) { + // TODO factory Pokemons pokemons = new Pokemons(data); + // TODO factory EncounterProbability encounterProbability = new EncounterProbability(probability); mEncounterNotification.show( diff --git a/app/src/main/java/com/icecream/snorlax/module/feature/encounter/EncounterNotification.java b/app/src/main/java/com/icecream/snorlax/module/feature/encounter/EncounterNotification.java index 97fa952..93b6105 100644 --- a/app/src/main/java/com/icecream/snorlax/module/feature/encounter/EncounterNotification.java +++ b/app/src/main/java/com/icecream/snorlax/module/feature/encounter/EncounterNotification.java @@ -19,7 +19,6 @@ import javax.inject.Inject; import javax.inject.Singleton; -import android.app.Application; import android.app.Notification; import android.app.NotificationManager; import android.content.Context; @@ -39,6 +38,8 @@ import com.icecream.snorlax.R; import com.icecream.snorlax.common.Strings; +import com.icecream.snorlax.module.context.pokemongo.PokemonGo; +import com.icecream.snorlax.module.context.snorlax.Snorlax; @Singleton final class EncounterNotification { @@ -48,10 +49,10 @@ final class EncounterNotification { private final NotificationManager mNotificationManager; @Inject - EncounterNotification(Context context, Application application) { + EncounterNotification(@Snorlax Context context, @Snorlax Resources resources, @PokemonGo NotificationManager notificationManager) { mContext = context; - mResources = context.getResources(); - mNotificationManager = (NotificationManager) application.getSystemService(Context.NOTIFICATION_SERVICE); + mResources = resources; + mNotificationManager = notificationManager; } @SuppressWarnings("deprecation") diff --git a/app/src/main/java/com/icecream/snorlax/module/feature/encounter/EncounterPreferences.java b/app/src/main/java/com/icecream/snorlax/module/feature/encounter/EncounterPreferences.java index 1081bfc..3cca2a0 100644 --- a/app/src/main/java/com/icecream/snorlax/module/feature/encounter/EncounterPreferences.java +++ b/app/src/main/java/com/icecream/snorlax/module/feature/encounter/EncounterPreferences.java @@ -19,10 +19,10 @@ import javax.inject.Inject; import javax.inject.Singleton; -import android.content.Context; import android.content.res.Resources; import com.icecream.snorlax.R; +import com.icecream.snorlax.module.context.snorlax.Snorlax; import de.robv.android.xposed.XSharedPreferences; import rx.Observable; @@ -34,8 +34,8 @@ final class EncounterPreferences { private final XSharedPreferences mPreferences; @Inject - EncounterPreferences(Context context, XSharedPreferences preferences) { - mResources = context.getResources(); + EncounterPreferences(@Snorlax Resources resources, XSharedPreferences preferences) { + mResources = resources; mPreferences = preferences; } diff --git a/app/src/main/java/com/icecream/snorlax/module/feature/mitm/Mitm.java b/app/src/main/java/com/icecream/snorlax/module/feature/mitm/Mitm.java index 2962856..3430230 100644 --- a/app/src/main/java/com/icecream/snorlax/module/feature/mitm/Mitm.java +++ b/app/src/main/java/com/icecream/snorlax/module/feature/mitm/Mitm.java @@ -54,6 +54,7 @@ public void subscribe() throws Exception { mUnhookInputStream = XposedHelpers.findAndHookMethod(http, "getInputStream", new XC_MethodHook() { @Override protected void afterHookedMethod(MethodHookParam param) throws Throwable { + // TODO factory param.setResult(new MitmInputStream((InputStream) param.getResult())); } }); @@ -61,6 +62,7 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable { mUnhookOutputStream = XposedHelpers.findAndHookMethod(http, "getOutputStream", new XC_MethodHook() { @Override protected void afterHookedMethod(MethodHookParam param) throws Throwable { + // TODO factory param.setResult(new MitmOutputStream((OutputStream) param.getResult())); } }); diff --git a/app/src/main/java/com/icecream/snorlax/module/feature/mock/MockPreferences.java b/app/src/main/java/com/icecream/snorlax/module/feature/mock/MockPreferences.java index 1dc9edf..79cf79b 100644 --- a/app/src/main/java/com/icecream/snorlax/module/feature/mock/MockPreferences.java +++ b/app/src/main/java/com/icecream/snorlax/module/feature/mock/MockPreferences.java @@ -19,10 +19,10 @@ import javax.inject.Inject; import javax.inject.Singleton; -import android.content.Context; import android.content.res.Resources; import com.icecream.snorlax.R; +import com.icecream.snorlax.module.context.snorlax.Snorlax; import de.robv.android.xposed.XSharedPreferences; @@ -33,8 +33,8 @@ final class MockPreferences { private final XSharedPreferences mPreferences; @Inject - MockPreferences(Context context, XSharedPreferences preferences) { - mResources = context.getResources(); + MockPreferences(@Snorlax Resources resources, XSharedPreferences preferences) { + mResources = resources; mPreferences = preferences; }