Skip to content

Commit

Permalink
Code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
igoticecream committed Sep 25, 2016
1 parent e2ab275 commit 1e6c261
Show file tree
Hide file tree
Showing 17 changed files with 295 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 0 additions & 13 deletions app/src/main/java/com/icecream/snorlax/module/SnorlaxModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2016. Pedro Diaz <igoticecream@gmail.com>
*
* 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 {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2016. Pedro Diaz <igoticecream@gmail.com>
*
* 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2016. Pedro Diaz <igoticecream@gmail.com>
*
* 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");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2016. Pedro Diaz <igoticecream@gmail.com>
*
* 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2016. Pedro Diaz <igoticecream@gmail.com>
*
* 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 {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2016. Pedro Diaz <igoticecream@gmail.com>
*
* 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2016. Pedro Diaz <igoticecream@gmail.com>
*
* 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
Loading

0 comments on commit 1e6c261

Please sign in to comment.