Skip to content

Instantly share code, notes, and snippets.

@ricardobrg
Last active May 7, 2017 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricardobrg/aa21c7dd900718fd72684103a7140357 to your computer and use it in GitHub Desktop.
Save ricardobrg/aa21c7dd900718fd72684103a7140357 to your computer and use it in GitHub Desktop.
Helper methods for Android Apps
package br.com.brgweb.utilsexample;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class Utils {
/**
* Creates an ACTION_SEND intent to message the app Google Play link
*
* @param v View the View that called the method
* @param message String the message that will prepend the link
*/
public static void SendIntent(View v, String message) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, message + "https://play.google.com/store/apps/details?id=" + v.getContext().getPackageName());
sendIntent.setType("text/plain");
v.getContext().startActivity(sendIntent);
}
/**
* Creates an ACTION_SEND intent to share the app Google Play Link
*
* @param v View the View that called the method
* @param message String the message that will prepend the link
*/
public static void ShareIntent(View v, String message) {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
ApplicationInfo applicationInfo = context.getApplicationInfo();
int stringId = applicationInfo.labelRes;
String appName = stringId == 0 ? applicationInfo.nonLocalizedLabel.toString() : context.getString(stringId);
sharingIntent.setType("text/plain");
String shareBody = message + "https://play.google.com/store/apps/details?id=" + v.getContext().getPackageName())
;
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, appName);
sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
v.getContext().startActivity(Intent.createChooser(sharingIntent, "Compartilhar"));
}
/**
* Send the user to Google Play Store to rate the app
*
* @param v View the View that called the method
*/
public static void RateIntent(View v) {
Uri uri = Uri.parse("market://details?id=" + v.getContext().getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
try {
v.getContext().startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
Toast.makeText(v.getContext(), "Não foi possível iniciar a Play Store.", Toast.LENGTH_SHORT).show();
}
}
/**
* Start activity to send an email with a bug report
*
* @param v View the View that called the method
*/
public static void BugReportIntent(final View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setTitle("Avisar Erro no App");
final String[] message_text = new String[1];
// Set up the input
final EditText input = new EditText(v.getContext());
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
message_text[0] = input.getText().toString();
ApplicationInfo applicationInfo = context.getApplicationInfo();
int stringId = applicationInfo.labelRes;
String appname = stringId == 0 ? applicationInfo.nonLocalizedLabel.toString() : context.getString(stringId);
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"android@brgweb.com.br"});
i.putExtra(Intent.EXTRA_SUBJECT, "[" + appname + "] Aviso de Erro");
i.putExtra(Intent.EXTRA_TEXT, message_text[0]);
try {
v.getContext().startActivity(Intent.createChooser(i, "Enviar email..."));
} catch (ActivityNotFoundException ex) {
Toast.makeText(v.getContext(), "Nenhum aplicativo de email encontrado. Por favor envie mensagem na Play Store", Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
/**
* Logout function that clear all Paper.book data and restarts main activity
*
* @param v View the View that called the method
*/
public static void Logout(View v) {
Paper.init();
Paper.book.destroy();
Intent intent = getIntent();
finish();
startActivity(intent);
}
/**
* Check if app has a specific permission granted
*
* @param permission String the permission name
* @param a Acitivity the activity from wich the method was called
* @return boolean if the permission was granted
*/
public boolean hasPermission(String permission, Activity a) {
int permissionCheck = ContextCompat.checkSelfPermission(a, permission);
return permissionCheck == PackageManager.PERMISSION_GRANTED;
}
/**
* Check if the user has internet connection
*
* @param c Context the Context that called the method
* @return boolean if the user has connection
*/
public static boolean hasConnection(Context c) {
ConnectivityManager cm = (ConnectivityManager) c.getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) {
if ((activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) ||
(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)) {
return true;
}
} else {
return false;
}
return false;
}
/**
*
* Shows a dialog to configure network. If the user doesn't configure, it closes the app.
*
* @param message String the message to show in the dialog
* @param v View the view that called the method
* @param close boolean should the app be closed if user cancel the dialog
*/
public static void connectionDialog(String message, View v, boolean close){
android.support.v7.app.AlertDialog.Builder alertDialogBuilder = new android.support.v7.app.AlertDialog.Builder(v);
alertDialogBuilder.setMessage(message)
.setCancelable(false)
.setPositiveButton("Configurar",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
}
})
.setNegativeButton("Cancelar",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
if (close) {
closeApp(getActivity());
}
}
});
android.support.v7.app.AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
/**
*
* Finish activity and close the App
*
* @param a Activity the activity
*/
public static void closeApp(Activity a){
if (android,os.Build.VERSION_SDK_INT <= 15){
a.finish();
System.exit(0);
}else if (android,os.Build.VERSION_SDK_INT <= 20){
a.finishAffinity();
}else if (android,os.Build.VERSION_SDK_INT >= 21){
a.finishAndRemoveTask();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment