Created
May 14, 2018 18:19
-
-
Save GrakovNe/3b0710685cdb4a40d10529f58fb7a259 to your computer and use it in GitHub Desktop.
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 org.grakovne.mds.android.utils; | |
| import android.content.BroadcastReceiver; | |
| import android.content.Context; | |
| import android.content.Intent; | |
| import android.content.pm.PackageManager; | |
| import android.text.TextUtils; | |
| import android.widget.Toast; | |
| import org.grakovne.mds.android.R; | |
| import org.grakovne.mds.android.common.BroadcastActionNames.Application; | |
| import org.grakovne.mds.android.manager.BroadcastManager; | |
| import org.grakovne.mds.android.manager.SettingsManager; | |
| import static java.util.Arrays.asList; | |
| import static org.grakovne.mds.android.manager.BroadcastManager.subscribeTo; | |
| import static org.grakovne.mds.android.manager.BroadcastManager.unSubscribeFrom; | |
| public class CommonUtils { | |
| public static boolean isVkAppInstalled(Context context) { | |
| return isAppInstalled(context, "com.vkontakte.android"); | |
| } | |
| private static boolean isAppInstalled(Context context, String packageName) { | |
| try { | |
| context.getPackageManager().getApplicationInfo(packageName, 0); | |
| return true; | |
| } | |
| catch (PackageManager.NameNotFoundException e) { | |
| return false; | |
| } | |
| } | |
| public static void ifUserLogged(Context context, Action positiveAction, Action negativeAction) { | |
| if (SettingsManager.getManager(context).isUserLogged()){ | |
| positiveAction.apply(); | |
| } else { | |
| negativeAction.apply(); | |
| } | |
| } | |
| public static void ifUserLogged(Context context, Action action) { | |
| ifUserLogged(context, action, new Action() { | |
| @Override | |
| public void apply() { | |
| BroadcastReceiver receiver = new BroadcastReceiver() { | |
| @Override | |
| public void onReceive(Context context, Intent intent) { | |
| String requiredAction = intent.getAction(); | |
| if (TextUtils.isEmpty(requiredAction)) { | |
| return; | |
| } | |
| unSubscribeFrom(context, this); | |
| switch (requiredAction) { | |
| case Application.USER_LOGIN_EVENT: | |
| action.apply(); | |
| break; | |
| } | |
| } | |
| }; | |
| subscribeTo(context, | |
| receiver, | |
| asList(Application.USER_LOGIN_EVENT, Application.USER_LOGIN_CANCELLED_EVENT)); | |
| Toast.makeText(context, R.string.sign_in_reqiured_toast, Toast.LENGTH_LONG).show(); | |
| BroadcastManager.send(context, Application.USER_LOGIN_INTENT); | |
| } | |
| }); | |
| } | |
| public static <T> void ifNotNull(T value, Action action) { | |
| if (null != value) { | |
| action.apply(); | |
| } | |
| } | |
| public interface Action { | |
| void apply(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment