Search in sources :

Example 1 with Pushy

use of me.pushy.sdk.Pushy in project NotiSender by choiman1559.

the class MainPreference method onCreatePreferences.

@SuppressLint("HardwareIds")
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    setPreferencesFromResource(R.xml.root_preferences, rootKey);
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestIdToken(getString(R.string.default_web_client_id)).requestEmail().build();
    mGoogleSignInClient = GoogleSignIn.getClient(mContext, gso);
    mAuth = FirebaseAuth.getInstance();
    mFirebaseFirestore = FirebaseFirestore.getInstance();
    prefs = mContext.getSharedPreferences("com.noti.main_preferences", MODE_PRIVATE);
    logPrefs = mContext.getSharedPreferences("com.noti.main_logs", MODE_PRIVATE);
    mFirebaseFirestore.collection("ApiKey").get().addOnCompleteListener(task -> {
        if (task.isSuccessful() && task.getResult() != null) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                prefs.edit().putString("Latest_Version_Play", document.getString("Version_Play")).putString("ApiKey_FCM", document.getString("FCM")).putString("ApiKey_Pushy", document.getString("Pushy")).putString("ApiKey_Billing", document.getString("Billing")).apply();
            }
        } else {
            new MaterialAlertDialogBuilder(mContext).setTitle("Error occurred!").setMessage("Error occurred while initializing client token.\nplease check your internet connection and try again.").setPositiveButton("OK", (dialog, which) -> mContext.finishAndRemoveTask()).setCancelable(false);
        }
    });
    if (prefs.getString("FirebaseIIDPrefix", "").isEmpty()) {
        FirebaseInstallations.getInstance().getId().addOnCompleteListener(task -> {
            if (task.isSuccessful())
                prefs.edit().putString("FirebaseIIDPrefix", task.getResult()).apply();
        });
    }
    if (prefs.getString("AndroidIDPrefix", "").isEmpty()) {
        prefs.edit().putString("AndroidIDPrefix", Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.ANDROID_ID)).apply();
    }
    if (prefs.getString("GUIDPrefix", "").isEmpty()) {
        prefs.edit().putString("GUIDPrefix", UUID.randomUUID().toString()).apply();
    }
    if (prefs.getString("MacIDPrefix", "").isEmpty()) {
        String interfaceName = "wlan0";
        try {
            List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
            for (NetworkInterface intf : interfaces) {
                if (!intf.getName().equalsIgnoreCase(interfaceName))
                    continue;
                byte[] mac = intf.getHardwareAddress();
                if (mac == null) {
                    prefs.edit().putString("MacIDPrefix", "unknown").apply();
                    break;
                }
                StringBuilder buf = new StringBuilder();
                for (byte b : mac) buf.append(String.format("%02X:", b));
                if (buf.length() > 0)
                    buf.deleteCharAt(buf.length() - 1);
                prefs.edit().putString("MacIDPrefix", buf.toString()).apply();
                break;
            }
        } catch (Exception e) {
            prefs.edit().putString("MacIDPrefix", "unknown").apply();
        }
    }
    Login = findPreference("Login");
    TestRun = findPreference("testNoti");
    Service = findPreference("service");
    ServiceToggle = findPreference("serviceToggle");
    Server = findPreference("server");
    Subscribe = findPreference("Subscribe");
    ServerInfo = findPreference("ServerInfo");
    ForWearOS = findPreference("forWear");
    FindPhone = findPreference("findPhone");
    pairDevice = findPreference("pairDevice");
    mBillingHelper = BillingHelper.initialize(mContext, new BillingHelper.BillingCallback() {

        @Override
        public void onPurchased(String productId) {
            switch(productId) {
                case BillingHelper.SubscribeID:
                    ToastHelper.show(mContext, "Thanks for purchase!", "OK", ToastHelper.LENGTH_SHORT);
                    ServiceToggle.setEnabled(!prefs.getString("UID", "").equals(""));
                    ServiceToggle.setSummary("");
                    Subscribe.setVisible(false);
                    new RegisterForPushNotificationsAsync().execute();
                    break;
                case BillingHelper.DonateID:
                    MaterialAlertDialogBuilder dialog = new MaterialAlertDialogBuilder(new ContextThemeWrapper(mContext, R.style.MaterialAlertDialog_Material3));
                    dialog.setTitle("Thank you for your donation!");
                    dialog.setMessage("This donation will be used to improve Noti Sender!");
                    dialog.setIcon(R.drawable.ic_fluent_gift_24_regular);
                    dialog.setCancelable(false);
                    dialog.setPositiveButton("Close", (dialogInterface, i) -> {
                    });
                    dialog.show();
                    break;
            }
        }

        @Override
        public void onUpdatePrice(Double priceValue) {
        }
    });
    if (mBillingHelper.isSubscribed()) {
        new RegisterForPushNotificationsAsync().execute();
    }
    boolean ifUIDBlank = prefs.getString("UID", "").equals("");
    if (!ifUIDBlank) {
        Login.setSummary("Logined as " + prefs.getString("Email", ""));
        Login.setTitle(R.string.Logout);
        if (prefs.getString("Email", "").equals("") && mAuth.getCurrentUser() != null)
            prefs.edit().putString("Email", mAuth.getCurrentUser().getEmail()).apply();
        if (prefs.getString("server", "Firebase Cloud Message").equals("Pushy")) {
            if (mBillingHelper.isSubscribed()) {
                Subscribe.setVisible(false);
                ServiceToggle.setEnabled(true);
            } else {
                Subscribe.setVisible(true);
                ServiceToggle.setEnabled(false);
                ServiceToggle.setSummary("Needs subscribe to use Pushy server");
            }
        } else {
            Subscribe.setVisible(false);
            ServiceToggle.setEnabled(true);
        }
    } else {
        ServiceToggle.setEnabled(false);
        if (prefs.getString("server", "Firebase Cloud Message").equals("Pushy") && !mBillingHelper.isSubscribed()) {
            Subscribe.setVisible(true);
            ServiceToggle.setSummary("Needs subscribe to use Pushy server");
        } else
            Subscribe.setVisible(false);
    }
    prefs.registerOnSharedPreferenceChangeListener((p, k) -> {
        if (k.equals("serviceToggle")) {
            ((SwitchPreference) ServiceToggle).setChecked(prefs.getBoolean("serviceToggle", false));
        }
    });
    Service.setSummary("Now : " + prefs.getString("service", "not selected"));
    Service.setOnPreferenceChangeListener((p, n) -> {
        p.setSummary("Now : " + n.toString());
        return true;
    });
    Server.setSummary("Now : " + prefs.getString("server", "Firebase Cloud Message"));
    Server.setOnPreferenceChangeListener((p, n) -> {
        p.setSummary("Now : " + n.toString());
        if (n.toString().equals("Pushy")) {
            if (mBillingHelper.isSubscribed())
                ServiceToggle.setEnabled(true);
            else {
                Subscribe.setVisible(true);
                ServiceToggle.setEnabled(false);
                ServiceToggle.setSummary("Needs subscribe to use Pushy server");
            }
        } else {
            ServiceToggle.setEnabled(!prefs.getString("UID", "").equals(""));
            ServiceToggle.setSummary("");
            Subscribe.setVisible(false);
        }
        return true;
    });
    try {
        mContext.getPackageManager().getPackageInfo("com.google.android.wearable.app", 0);
    } catch (PackageManager.NameNotFoundException e) {
        ForWearOS.setVisible(false);
    }
    migrationHistory();
}
Also used : Bundle(android.os.Bundle) PackageManager(android.content.pm.PackageManager) NonNull(androidx.annotation.NonNull) Date(java.util.Date) MaterialAlertDialogBuilder(com.google.android.material.dialog.MaterialAlertDialogBuilder) Uri(android.net.Uri) GoogleSignInClient(com.google.android.gms.auth.api.signin.GoogleSignInClient) GoogleAuthProvider(com.google.firebase.auth.GoogleAuthProvider) GoogleSignIn(com.google.android.gms.auth.api.signin.GoogleSignIn) GoogleSignInResult(com.google.android.gms.auth.api.signin.GoogleSignInResult) View(android.view.View) GoogleSignInAccount(com.google.android.gms.auth.api.signin.GoogleSignInAccount) Log(android.util.Log) AppInfoActivity(com.noti.main.ui.AppInfoActivity) ConnectivityManager(android.net.ConnectivityManager) SettingsActivity.mBillingHelper(com.noti.main.SettingsActivity.mBillingHelper) SettingsActivity(com.noti.main.SettingsActivity) SwitchPreference(androidx.preference.SwitchPreference) NetworkInterface(java.net.NetworkInterface) NetworkInfo(android.net.NetworkInfo) UUID(java.util.UUID) ViewGroup(android.view.ViewGroup) List(java.util.List) Nullable(androidx.annotation.Nullable) ActivityResultContracts(androidx.activity.result.contract.ActivityResultContracts) ToastHelper(com.noti.main.ui.ToastHelper) MonetCompat(com.kieronquinn.monetcompat.core.MonetCompat) Context(android.content.Context) Intent(android.content.Intent) HistoryActivity(com.noti.main.ui.prefs.HistoryActivity) PairMainActivity(com.noti.main.ui.pair.PairMainActivity) PreferenceFragmentCompat(androidx.preference.PreferenceFragmentCompat) FragmentActivity(androidx.fragment.app.FragmentActivity) SuppressLint(android.annotation.SuppressLint) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) MODE_PRIVATE(android.content.Context.MODE_PRIVATE) Settings(android.provider.Settings) Notify(com.application.isradeleon.notify.Notify) Auth(com.google.android.gms.auth.api.Auth) FirebaseAuth(com.google.firebase.auth.FirebaseAuth) FirebaseInstallations(com.google.firebase.installations.FirebaseInstallations) ActivityResultLauncher(androidx.activity.result.ActivityResultLauncher) LayoutInflater(android.view.LayoutInflater) BillingHelper(com.noti.main.utils.BillingHelper) FirebaseMessaging(com.google.firebase.messaging.FirebaseMessaging) Preference(androidx.preference.Preference) ContextThemeWrapper(androidx.appcompat.view.ContextThemeWrapper) AuthCredential(com.google.firebase.auth.AuthCredential) FirebaseFirestore(com.google.firebase.firestore.FirebaseFirestore) OptionActivity(com.noti.main.ui.OptionActivity) R(com.noti.main.R) SharedPreferences(android.content.SharedPreferences) Pushy(me.pushy.sdk.Pushy) Activity(android.app.Activity) Collections(java.util.Collections) AsyncTask(com.noti.main.utils.AsyncTask) GoogleSignInOptions(com.google.android.gms.auth.api.signin.GoogleSignInOptions) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) SwitchPreference(androidx.preference.SwitchPreference) NetworkInterface(java.net.NetworkInterface) MaterialAlertDialogBuilder(com.google.android.material.dialog.MaterialAlertDialogBuilder) ContextThemeWrapper(androidx.appcompat.view.ContextThemeWrapper) PackageManager(android.content.pm.PackageManager) GoogleSignInOptions(com.google.android.gms.auth.api.signin.GoogleSignInOptions) SuppressLint(android.annotation.SuppressLint)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 Activity (android.app.Activity)1 Context (android.content.Context)1 MODE_PRIVATE (android.content.Context.MODE_PRIVATE)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 PackageManager (android.content.pm.PackageManager)1 ConnectivityManager (android.net.ConnectivityManager)1 NetworkInfo (android.net.NetworkInfo)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 Settings (android.provider.Settings)1 Log (android.util.Log)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 ActivityResultLauncher (androidx.activity.result.ActivityResultLauncher)1 ActivityResultContracts (androidx.activity.result.contract.ActivityResultContracts)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1