Skip to content

Commit

Permalink
added HatchNotification originally made by MaT-PT pperle/PoGoIV_xposed@
Browse files Browse the repository at this point in the history
…612649c

added translation of pokemon names
added Helper class to calc level and ivs
  • Loading branch information
pperle committed Sep 27, 2016
1 parent ca6aa02 commit 66206cd
Show file tree
Hide file tree
Showing 12 changed files with 906 additions and 1 deletion.
63 changes: 63 additions & 0 deletions app/src/main/java/com/icecream/snorlax/common/Helper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.icecream.snorlax.common;

import android.content.res.Resources;

import com.icecream.snorlax.R;

import POGOProtos.Data.PokemonDataOuterClass;

public class Helper {

private static String[] pokemonNames = null;

// from https://github.com/farkam135/GoIV/blob/master/app/src/main/java/com/kamron/pogoiv/Data.java#L8
private static final double[] CpM = {0.0939999967813492, 0.135137432089339, 0.166397869586945, 0.192650913155325, 0.215732470154762, 0.236572651424822, 0.255720049142838, 0.273530372106572, 0.290249884128571, 0.306057381389863
, 0.321087598800659, 0.335445031996451, 0.349212676286697, 0.362457736609939, 0.375235587358475, 0.387592407713878, 0.399567276239395, 0.4111935532161, 0.422500014305115, 0.432926420512509, 0.443107545375824
, 0.453059948165049, 0.46279838681221, 0.472336085311278, 0.481684952974319, 0.490855807179549, 0.499858438968658, 0.5087017489616, 0.517393946647644, 0.525942516110322, 0.534354329109192, 0.542635753803599
, 0.550792694091797, 0.558830584490385, 0.566754519939423, 0.57456912814537, 0.582278907299042, 0.589887907888945, 0.597400009632111, 0.604823648665171, 0.61215728521347, 0.619404107958234, 0.626567125320435
, 0.633649178748576, 0.6406529545784, 0.647580971386554, 0.654435634613037, 0.661219265805859, 0.667934000492096, 0.674581885647492, 0.681164920330048, 0.687684901255373, 0.694143652915955, 0.700542901033063
, 0.706884205341339, 0.713169074873823, 0.719399094581604, 0.725575586915154, 0.731700003147125, 0.734741038550429, 0.737769484519958, 0.740785579737136, 0.743789434432983, 0.746781197247765, 0.749761044979095
, 0.752729099732281, 0.75568550825119, 0.758630370209851, 0.761563837528229, 0.76448604959218, 0.767397165298462, 0.770297293677362, 0.773186504840851, 0.776064947064992, 0.778932750225067, 0.781790050767666
, 0.784636974334717, 0.787473608513275, 0.790300011634827};


public static float calcLevel(PokemonDataOuterClass.PokemonData pokemonData) {
if (pokemonData.getIsEgg())
return 0;
return calcLevel(pokemonData.getCpMultiplier() + pokemonData.getAdditionalCpMultiplier());
}

private static float calcLevel(float cpMultiplier) {
float level = 1;
for (double currentCpM : CpM) {
if (Math.abs(cpMultiplier - currentCpM) < 0.0001) {
return level;
}
level += 0.5;
}
return level;
}

public static double calcPotential(PokemonDataOuterClass.PokemonData encounteredPokemon) {
return (double) Math.round(((encounteredPokemon.getIndividualAttack() + encounteredPokemon.getIndividualDefense() + encounteredPokemon.getIndividualStamina()) / 45.0 * 100.0) * 10) / 10;
}

private static void loadPokemonNames(Resources resources) {
pokemonNames = resources.getStringArray(R.array.pokemon);
}

public static String[] getPokemonNames(Resources resources) {
if (pokemonNames == null) {
loadPokemonNames(resources);
}
return pokemonNames;
}

public static String getPokemonName(int pokemonNumber, Resources resources) {
String[] pokemonNames = getPokemonNames(resources);
if (pokemonNumber > 0 && pokemonNumber <= pokemonNames.length)
return getPokemonNames(resources)[pokemonNumber - 1];
else
return "(unknown Pokémon: " + pokemonNumber + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import android.view.View;

import com.icecream.snorlax.R;
import com.icecream.snorlax.common.Helper;
import com.icecream.snorlax.common.Strings;
import com.icecream.snorlax.module.context.pokemongo.PokemonGo;
import com.icecream.snorlax.module.context.snorlax.Snorlax;
Expand Down Expand Up @@ -70,7 +71,7 @@ void show(int pokemonNumber, String pokemonName, double iv, int attack, int defe
getLargeIconHeight(),
false
))
.setContentTitle(mContext.getString(R.string.notification_title, pokemonName))
.setContentTitle(mContext.getString(R.string.notification_title, Helper.getPokemonName(pokemonNumber, mResources)))
.setContentText(mContext.getString(R.string.notification_content, iv, attack, defense, stamina, cp, level, hp))
.setStyle(new NotificationCompat.InboxStyle()
.addLine(mContext.getString(R.string.notification_categoty_stats_content, iv, attack, defense, stamina, cp, level, hp))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.icecream.snorlax.module.feature.hatch;

class EggHatchRewards {
private int experienceAwarded;
private int candyAwarded;
private int stardustAwarded;

public EggHatchRewards(int experienceAwarded, int candyAwarded, int stardustAwarded) {
this.experienceAwarded = experienceAwarded;
this.candyAwarded = candyAwarded;
this.stardustAwarded = stardustAwarded;
}

public int getExperienceAwarded() {
return experienceAwarded;
}

public int getCandyAwarded() {
return candyAwarded;
}

public int getStardustAwarded() {
return stardustAwarded;
}
}
150 changes: 150 additions & 0 deletions app/src/main/java/com/icecream/snorlax/module/feature/hatch/Hatch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package com.icecream.snorlax.module.feature.hatch;

import android.support.v4.util.Pair;

import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import com.icecream.snorlax.module.feature.Feature;
import com.icecream.snorlax.module.feature.mitm.MitmRelay;
import com.icecream.snorlax.module.util.Log;
import com.icecream.snorlax.module.util.RxFuncitons;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Singleton;

import POGOProtos.Data.PokemonDataOuterClass;
import POGOProtos.Inventory.InventoryDeltaOuterClass;
import POGOProtos.Inventory.InventoryItemDataOuterClass;
import POGOProtos.Inventory.InventoryItemOuterClass;
import POGOProtos.Networking.Requests.RequestOuterClass;
import POGOProtos.Networking.Requests.RequestTypeOuterClass;
import POGOProtos.Networking.Responses.GetHatchedEggsResponseOuterClass;
import POGOProtos.Networking.Responses.GetInventoryResponseOuterClass;
import rx.Observable;
import rx.Subscription;

@Singleton
public final class Hatch implements Feature {

private final MitmRelay mMitmRelay;
private final HatchPreferences mPreferences;
private final HatchNotification mHatchNotification;
private Subscription mSubscription;

private final Map<Long, EggHatchRewards> hatchedEggs = new HashMap<>();

public Hatch(MitmRelay mitmRelay, HatchPreferences hatchPreferences, HatchNotification hatchNotification) {
this.mMitmRelay = mitmRelay;
this.mPreferences = hatchPreferences;
this.mHatchNotification = hatchNotification;
}

private void onHatch(ByteString bytes) {
try {
GetHatchedEggsResponseOuterClass.GetHatchedEggsResponse getHatchedEggsResponse = GetHatchedEggsResponseOuterClass.GetHatchedEggsResponse.parseFrom(bytes);

if (!getHatchedEggsResponse.getSuccess())
return;

for (int i = 0, l = getHatchedEggsResponse.getPokemonIdCount(); i < l; i++) {
EggHatchRewards eggHatchRewards = new EggHatchRewards(getHatchedEggsResponse.getExperienceAwarded(i),
getHatchedEggsResponse.getCandyAwarded(i),
getHatchedEggsResponse.getStardustAwarded(i));
hatchedEggs.put(getHatchedEggsResponse.getPokemonId(i), eggHatchRewards);
}

} catch (InvalidProtocolBufferException e) {
Log.d("GetHatchedEggsResponse failed: %s" + e.getMessage());
Log.e(e);
}
}


private void onInventoryUpdate(ByteString bytes) {
try {
GetInventoryResponseOuterClass.GetInventoryResponse getInventoryResponse = GetInventoryResponseOuterClass.GetInventoryResponse.parseFrom(bytes);

if (!getInventoryResponse.getSuccess() || !getInventoryResponse.hasInventoryDelta())
return;

// First time (delta.getOriginalTimestampMs() == 0L) the server sends the full inventory,
// after that only what has changed since the last request
InventoryDeltaOuterClass.InventoryDelta inventoryDelta = getInventoryResponse.getInventoryDelta();

List<EggHatchRewards> hatchedEggsAwardedList = new ArrayList();
List<PokemonDataOuterClass.PokemonData> hatchedEggsAwardedListPokemonData = new ArrayList();

for (InventoryItemOuterClass.InventoryItem inventoryItem : inventoryDelta.getInventoryItemsList()) {
if (inventoryItem.hasInventoryItemData()) {
InventoryItemDataOuterClass.InventoryItemData inventoryItemData = inventoryItem.getInventoryItemData();

if (inventoryItemData.hasPokemonData()) {
PokemonDataOuterClass.PokemonData pokemonData = inventoryItemData.getPokemonData();
long pokemonId = pokemonData.getId();

if (hatchedEggs.containsKey(pokemonId)) {
hatchedEggsAwardedList.add(hatchedEggs.get(pokemonId));
hatchedEggs.remove(pokemonId);

hatchedEggsAwardedListPokemonData.add(pokemonData);

}
}
}
}


} catch (InvalidProtocolBufferException e) {
Log.d("GetInventoryResponse failed: %s" + e.getMessage());
Log.e(e);
}
}

@Override
public void subscribe() {
unsubscribe();

mSubscription = mMitmRelay
.getObservable()
.compose(mPreferences.isEnabled())
.flatMap(envelope -> {
List<RequestOuterClass.Request> requests = envelope.getRequest().getRequestsList();

for (int i = 0; i < requests.size(); i++) {
RequestTypeOuterClass.RequestType type = requests.get(i).getRequestType();

switch (type) {
case GET_HATCHED_EGGS:
case GET_INVENTORY:
return Observable.just(new Pair<>(type, envelope.getResponse().getReturns(i)));
default:
break;
}

}
return Observable.empty();
})
.subscribe(pair -> {
switch (pair.first) {
case GET_HATCHED_EGGS:
onHatch(pair.second);
break;
case GET_INVENTORY:
onInventoryUpdate(pair.second);
break;
default:
break;
}
}
, Log::e);
}

@Override
public void unsubscribe() {
RxFuncitons.unsubscribe(mSubscription);
}
}
Loading

0 comments on commit 66206cd

Please sign in to comment.