Created
December 20, 2016 01:51
-
-
Save JosiasSena/6766502ceb8a529f0e3d6b126bbd453f to your computer and use it in GitHub Desktop.
Helpful utility class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package josiassena.humbug; | |
import android.Manifest; | |
import android.app.Activity; | |
import android.app.NotificationManager; | |
import android.content.ClipData; | |
import android.content.ClipboardManager; | |
import android.content.Context; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.content.pm.ActivityInfo; | |
import android.content.pm.PackageManager; | |
import android.content.res.Configuration; | |
import android.location.LocationManager; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.os.PowerManager; | |
import android.provider.Settings; | |
import android.support.annotation.NonNull; | |
import android.support.v4.app.ActivityCompat; | |
import android.support.v7.app.AlertDialog; | |
import android.telephony.TelephonyManager; | |
import android.util.Log; | |
import android.view.View; | |
import android.view.inputmethod.InputMethodManager; | |
import android.widget.Toast; | |
/** | |
* File created by josiassena on 12/19/16. | |
*/ | |
public final class Utils { | |
private static final String TAG = Utils.class.getSimpleName(); | |
private static final String CALL_PHONE_PERMISSION_EXCEPTION_TEXT = | |
"CALL_PHONE permission has not been granted to the application. Please add " + | |
"<uses-permission android:name=\"android.permission.CALL_PHONE\" /> " + | |
"to your AndroidManifest.xml."; | |
private Utils() { | |
// do nothing | |
} | |
public static void copyTextToClipboard(@NonNull final Context context, | |
@NonNull final String label, | |
@NonNull final String text) { | |
final ClipData clip = ClipData.newPlainText(label, text); | |
final ClipboardManager clipboard = (ClipboardManager) context | |
.getSystemService(Context.CLIPBOARD_SERVICE); | |
clipboard.setPrimaryClip(clip); | |
Toast.makeText(context, "Message copied to clipboard", Toast.LENGTH_SHORT).show(); | |
} | |
public static void dismissNotification(final Context context, int notificationId) { | |
final NotificationManager notificationManager = (NotificationManager) context | |
.getSystemService(Context.NOTIFICATION_SERVICE); | |
notificationManager.cancel(notificationId); | |
} | |
public static void showView(@NonNull final View view) { | |
if (view != null) { | |
view.setVisibility(View.VISIBLE); | |
} else { | |
Log.e(TAG, "hideView: the view was null. Cannot perform this action."); | |
} | |
} | |
public static void hideView(@NonNull final View view) { | |
if (view != null) { | |
view.setVisibility(View.GONE); | |
} else { | |
Log.e(TAG, "hideView: the view was null. Cannot perform this action."); | |
} | |
} | |
public static void hideSoftKeyboard(@NonNull Activity activity) { | |
final InputMethodManager inputMethodManager = (InputMethodManager) activity | |
.getSystemService(Activity.INPUT_METHOD_SERVICE); | |
final View currentFocus = activity.getCurrentFocus(); | |
if (currentFocus != null) { | |
inputMethodManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0); | |
} | |
} | |
public static void hideSoftKeyboard(@NonNull final Context context, @NonNull final View view) { | |
final InputMethodManager inputMethodManager = (InputMethodManager) context | |
.getSystemService(Context.INPUT_METHOD_SERVICE); | |
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0); | |
} | |
public static void wakeUpPhone(@NonNull Context context, @NonNull String tag) { | |
final PowerManager powerManager = (PowerManager) context | |
.getSystemService(Context.POWER_SERVICE); | |
final PowerManager.WakeLock wakeLock = powerManager.newWakeLock(( | |
PowerManager.SCREEN_BRIGHT_WAKE_LOCK | | |
PowerManager.FULL_WAKE_LOCK | | |
PowerManager.ACQUIRE_CAUSES_WAKEUP), tag); | |
wakeLock.acquire(); | |
} | |
public static String getMyPhoneNumber(@NonNull Context context) { | |
final TelephonyManager telephonyManager = (TelephonyManager) context | |
.getSystemService(Context.TELEPHONY_SERVICE); | |
return telephonyManager.getLine1Number(); | |
} | |
public static String getDeviceName() { | |
return Build.MODEL; | |
} | |
public static String getDeviceId() { | |
return Build.SERIAL; | |
} | |
public static void callPhone(@NonNull final Context context, | |
@NonNull final String phoneNumber) { | |
final Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber)); | |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) == | |
PackageManager.PERMISSION_GRANTED) { | |
context.startActivity(intent); | |
} else { | |
Log.e(TAG, "callPhone: " + CALL_PHONE_PERMISSION_EXCEPTION_TEXT); | |
} | |
} | |
public static boolean isGpsEnabled(@NonNull final Context context) { | |
final LocationManager locationManager = (LocationManager) context | |
.getSystemService(Context.LOCATION_SERVICE); | |
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); | |
} | |
public static void goToGpsSettings(@NonNull final Context context) { | |
final Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); | |
context.startActivity(intent); | |
} | |
public static void goToApplicationSettings(@NonNull final Context context) { | |
final Uri uri = Uri.fromParts("package", context.getPackageName(), null); | |
final Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, uri); | |
context.startActivity(intent); | |
} | |
public static boolean isTextContainsSpecialCharsOrNumbers(@NonNull String text) { | |
return !text.matches("^[a-z A-Z]+$"); | |
} | |
public static String removeWhiteSpacesFromString(@NonNull String text) { | |
return text.replaceAll("\\s+", ""); | |
} | |
public static void unlockScreenOrientation(@NonNull Activity activity) { | |
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); | |
} | |
public static void lockScreenOrientation(@NonNull Activity activity) { | |
final int currentOrientation = activity.getResources().getConfiguration().orientation; | |
if (currentOrientation == Configuration.ORIENTATION_PORTRAIT) { | |
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); | |
} else { | |
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); | |
} | |
} | |
public static void showNormalOKAlertDialog(@NonNull final Context context, | |
@NonNull final String dialogTitle, | |
@NonNull final String dialogBody) { | |
new AlertDialog.Builder(context) | |
.setCancelable(false) | |
.setTitle(dialogTitle) | |
.setMessage(dialogBody) | |
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(final DialogInterface dialog, final int which) { | |
dialog.dismiss(); | |
} | |
}).show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment