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();
}
Aggregations