Thursday, May 25, 2017

Android - redirect user to play store for installing app

Intent viewIntent =
new Intent("android.intent.action.VIEW",
Uri.parse("https://play.google.com/store/apps/details?id=com.google.android.apps.ondemand.consumer"));
_mainActivity.startActivity(viewIntent);

Android - uninstall app programmatically

private void uninstallPackage(String name) {
Intent intent = new Intent(Intent.ACTION_DELETE);
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
intent.setData(Uri.parse("package:"+name));
startActivity(intent);
}

private boolean doesPackageExist(String name){
PackageManager pm=getPackageManager();
try {
PackageInfo info=pm.getPackageInfo(name,PackageManager.GET_META_DATA);
} catch (PackageManager.NameNotFoundException e) {
return false;
}
return true;
}

private void checkUninstallStatus(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
Log.d("TAG", "onActivityResult: user accepted the (un)install");
} else if (resultCode == RESULT_CANCELED) {
Log.d("TAG", "onActivityResult: user canceled the (un)install");
} else if (resultCode == RESULT_FIRST_USER) {
Log.d("TAG", "onActivityResult: failed to (un)install");
}
}
}

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
checkUninstallStatus(requestCode,resultCode,data);
}

Tuesday, May 23, 2017

Probabilistic algorithms

https://www.safaribooksonline.com/oriole/probabilistic-data-structures-in-python

Set Cardinality - HyperLogLog
Set Membership - With False positives but no False negatives - Bloom Filter
Set similarity (document similarity) - MinHash with Jaccard
Frequency Summaries (Leaderboards in games) - Count-min sketch
Streaming Quantiles - stream of data where you have to aggregate metrics without stopping - T-Digest (like min/max/percentile)



Friday, May 19, 2017

Java print current stack trace

StringWriter sw = new StringWriter();
new Throwable().printStackTrace(new PrintWriter(sw));

//And to actually print it
Log.e("tag","Current stack trace is:\n" + sw.toString());

Friday, May 12, 2017

Tuesday, April 25, 2017

Java - iterate over a UTF-8 string

Either read the file as UTF-8 or convert it later.


public static void main(String[] args) throws Exception {

String path = "D:\\test.txt";
FileInputStream stream = new FileInputStream(new File(path));
BufferedReader br = new BufferedReader(new InputStreamReader(stream,"UTF-8"));
String str;
while ((str = br.readLine()) != null) {
System.out.println(str.length());
for(int i = 0; i< str.length(); ++i) {
System.out.println(String.format("%04x",(int)str.charAt(i)));
}
}
}

OR

BufferedReader br = new BufferedReader(new InputStreamReader(stream));
String str;
while ((str = br.readLine()) != null) {
byte[] ptext = str.getBytes(ISO_8859_1);
str = new String(ptext, UTF_8);
System.out.println(str.length());
for(int i = 0; i< str.length(); ++i) {
System.out.println(String.format("%04x",(int)str.charAt(i)));
}
}

Java get unicode point of a character

class Main {
  public static void main(String[] args) {
char ch = 'ö';
System.out.println(getUnicodePoint(ch));
ch = 'म';
System.out.println(getUnicodePoint(ch));



  }

  private static String getUnicodePoint(char ch) {
      String hex = String.format("%04x", (int) ch);
       return hex;
  }
}

Blog Archive