Skip to content

Commit

Permalink
fix: auto update check
Browse files Browse the repository at this point in the history
Co-authored-by: tehcneko <[email protected]>
  • Loading branch information
omg-xtao and tehcneko committed Feb 11, 2024
1 parent c4fdd8d commit 91f289c
Show file tree
Hide file tree
Showing 13 changed files with 1,235 additions and 154 deletions.
3 changes: 3 additions & 0 deletions TMessagesProj/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ android {
buildConfigField "int", "OFFICIAL_VERSION_CODE", officialCode + ""
buildConfigField "int", "APP_ID", appId
buildConfigField "String", "APP_HASH", "\"" + appHash + "\""
buildConfigField "int", "IS_NO_GCM", "0"

externalNativeBuild {
cmake {
Expand Down Expand Up @@ -184,6 +185,8 @@ android {
proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
matchingFallbacks = ["release", "debug"]
signingConfig signingConfigs.release

buildConfigField "int", "IS_NO_GCM", "1"
}

release {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.PowerManager;
Expand All @@ -35,6 +36,7 @@
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;
import androidx.multidex.MultiDex;

import androidx.multidex.MultiDex;
Expand All @@ -43,8 +45,11 @@
import org.telegram.tgnet.ConnectionsManager;
import org.telegram.tgnet.TLRPC;
import org.telegram.ui.ActionBar.Theme;
import org.telegram.ui.Components.AlertsCreator;
import org.telegram.ui.Components.ForegroundDetector;
import org.telegram.ui.Components.Premium.boosts.BoostRepository;
import org.telegram.ui.Components.UpdateAppAlertDialog;
import org.telegram.ui.Components.UpdateLayout;
import org.telegram.ui.IUpdateLayout;
import org.telegram.ui.LauncherIconController;

Expand Down Expand Up @@ -658,19 +663,50 @@ protected void logDualCameraInternal(boolean success, boolean vendor) {
}

public boolean checkApkInstallPermissions(final Context context) {
return false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !ApplicationLoader.applicationContext.getPackageManager().canRequestPackageInstalls()) {
AlertsCreator.createApkRestrictedDialog(context, null).show();
return false;
}
return true;
}

public boolean openApkInstall(Activity activity, TLRPC.Document document) {
return false;
boolean exists = false;
try {
String fileName = FileLoader.getAttachFileName(document);
File f = FileLoader.getInstance(UserConfig.selectedAccount).getPathToAttach(document, true);
if (exists = f.exists()) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

if (Build.VERSION.SDK_INT >= 24) {
intent.setDataAndType(FileProvider.getUriForFile(activity, ApplicationLoader.getApplicationId() + ".provider", f), "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(f), "application/vnd.android.package-archive");
}
try {
activity.startActivityForResult(intent, 500);
} catch (Exception e) {
FileLog.e(e);
}
}
} catch (Exception e) {
FileLog.e(e);
}
return exists;
}

public boolean showUpdateAppPopup(Context context, TLRPC.TL_help_appUpdate update, int account) {
return false;
try {
(new UpdateAppAlertDialog(context, update, account)).show();
} catch (Exception e) {
FileLog.e(e);
}
return true;
}

public IUpdateLayout takeUpdateLayout(Activity activity, ViewGroup sideMenu, ViewGroup sideMenuContainer) {
return null;
return new UpdateLayout(activity, sideMenu, sideMenuContainer);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class BuildVars {
public static boolean isFdroid = BuildConfig.BUILD_TYPE.toLowerCase().contains("fdroid");
public static boolean isMini = !BuildConfig.FLAVOR.startsWith("full");
public static boolean isGServicesCompiled = BuildConfig.BUILD_TYPE.equals("debug") || BuildConfig.BUILD_TYPE.equals("release");
public static boolean isNoGCM = BuildConfig.IS_NO_GCM == 1;

static {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.util.Arrays;
import java.util.HashMap;

import tw.nekomimi.nekogram.helpers.remote.UpdateHelper;

public class FileRefController extends BaseController {

private static class Requester {
Expand Down Expand Up @@ -466,16 +468,25 @@ private void requestReferenceFromServer(Object parentObject, String locationKey,
}
favStickersWaiter.add(new Waiter(locationKey, parentKey));
} else if ("update".equals(string)) {
TLRPC.TL_help_getAppUpdate req = new TLRPC.TL_help_getAppUpdate();
try {
req.source = ApplicationLoader.applicationContext.getPackageManager().getInstallerPackageName(ApplicationLoader.applicationContext.getPackageName());
} catch (Exception ignore) {

}
if (req.source == null) {
req.source = "";
}
getConnectionsManager().sendRequest(req, (response, error) -> onRequestComplete(locationKey, parentKey, response, error, true, false));
// TLRPC.TL_help_getAppUpdate req = new TLRPC.TL_help_getAppUpdate();
// try {
// req.source = ApplicationLoader.applicationContext.getPackageManager().getInstallerPackageName(ApplicationLoader.applicationContext.getPackageName());
// } catch (Exception ignore) {
//
// }
// if (req.source == null) {
// req.source = "";
// }
// getConnectionsManager().sendRequest(req, (response, error) -> onRequestComplete(locationKey, parentKey, response, error, true, false));
UpdateHelper.getInstance().checkNewVersionAvailable((response, error) -> {
if (error != null) {
TLRPC.TL_error error1 = new TLRPC.TL_error();
error1.text = error;
onRequestComplete(locationKey, parentKey, response, error1, true, false);
} else {
onRequestComplete(locationKey, parentKey, response, null, true, false);
}
});
} else if (string.startsWith("avatar_")) {
long id = Utilities.parseLong(string);
if (id > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1667,29 +1667,26 @@ public static boolean isAppUpdateAvailable() {
}

public static boolean setNewAppVersionAvailable(TLRPC.TL_help_appUpdate update) {
String updateVersionString = null;
//String updateVersionString = null;
int versionCode = 0;
try {
PackageInfo packageInfo = ApplicationLoader.applicationContext.getPackageManager().getPackageInfo(ApplicationLoader.applicationContext.getPackageName(), 0);
versionCode = packageInfo.versionCode;
updateVersionString = packageInfo.versionName;
//updateVersionString = packageInfo.versionName;
} catch (Exception e) {
FileLog.e(e);
}
if (versionCode == 0) {
versionCode = buildVersion();
}
if (updateVersionString == null) {
updateVersionString = BuildVars.BUILD_VERSION_STRING;
}
if (update == null || update.version == null || versionBiggerOrEqual(updateVersionString, update.version)) {
pendingAppUpdate = null;
pendingAppUpdateBuildVersion = 0;
saveConfig();
return false;
}
//if (updateVersionString == null) {
// updateVersionString = BuildVars.BUILD_VERSION_STRING;
//}
//if (update.version == null || versionBiggerOrEqual(updateVersionString, update.version)) {
// return false;
//}
pendingAppUpdate = update;
pendingAppUpdateBuildVersion = BuildConfig.VERSION_CODE;
pendingAppUpdateBuildVersion = versionCode;
saveConfig();
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

import java.util.Locale;

import tw.nekomimi.nekogram.helpers.remote.UpdateHelper;

public class BlockingUpdateView extends FrameLayout implements NotificationCenter.NotificationCenterDelegate {

private TextView textView;
Expand All @@ -63,10 +65,8 @@ public BlockingUpdateView(final Context context) {
FrameLayout view = new FrameLayout(context);
addView(view, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, AndroidUtilities.dp(176) + (Build.VERSION.SDK_INT >= 21 ? AndroidUtilities.statusBarHeight : 0)));

RLottieImageView imageView = new RLottieImageView(context);
imageView.setAnimation(R.raw.qr_code_logo, 108, 108);
imageView.playAnimation();
imageView.getAnimatedDrawable().setAutoRepeat(1);
ImageView imageView = new ImageView(context);
imageView.setImageResource(R.mipmap.ic_launcher);
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setPadding(0, 0, 0, AndroidUtilities.dp(14));
view.addView(imageView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER, 0, top, 0, 0));
Expand Down Expand Up @@ -94,15 +94,14 @@ public BlockingUpdateView(final Context context) {
titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
titleTextView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
titleTextView.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
titleTextView.setText(LocaleController.getString("UpdateTelegram", R.string.UpdateTelegram));
titleTextView.setText(LocaleController.getString("UpdateTelegram", R.string.UpdateTelegram).replace("Telegram", LocaleController.getString("NekoX", R.string.NekoX)));
container.addView(titleTextView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL | Gravity.TOP));

textView = new TextView(context);
textView.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));
textView.setLinkTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteLinkText));
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
textView.setMovementMethod(new AndroidUtilities.LinkMovementMethodMy());
textView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
textView.setGravity(Gravity.LEFT | Gravity.TOP);
textView.setLineSpacing(AndroidUtilities.dp(2), 1.0f);
container.addView(textView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.LEFT | Gravity.TOP, 0, 44, 0, 0));

Expand Down Expand Up @@ -139,24 +138,14 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
acceptButton.setPadding(AndroidUtilities.dp(34), 0, AndroidUtilities.dp(34), 0);
addView(acceptButton, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, 46, Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 0, 0, 45));
acceptButton.setOnClickListener(view1 -> {
// if (ApplicationLoader.isStandaloneBuild() || BuildVars.DEBUG_VERSION) {
// if (!ApplicationLoader.applicationLoaderInstance.checkApkInstallPermissions(getContext())) {
// return;
// }
// if (appUpdate.document instanceof TLRPC.TL_document) {
// if (!ApplicationLoader.applicationLoaderInstance.openApkInstall((Activity) getContext(), appUpdate.document)) {
// FileLoader.getInstance(accountNum).loadFile(appUpdate.document, "update", FileLoader.PRIORITY_HIGH, 1);
// showProgress(true);
// }
// } else if (appUpdate.url != null) {
// Browser.openUrl(getContext(), appUpdate.url);
// }
// } else if (BuildVars.isHuaweiStoreApp()){
// Browser.openUrl(context, BuildVars.HUAWEI_STORE_URL);
// } else {
// Browser.openUrl(context, BuildVars.PLAYSTORE_APP_URL);
// }
Browser.openUrl(context, BuildVars.GITHUB_RELEASE_URL);
if (appUpdate.document instanceof TLRPC.TL_document) {
if (!ApplicationLoader.applicationLoaderInstance.openApkInstall((Activity) getContext(), appUpdate.document)) {
FileLoader.getInstance(accountNum).loadFile(appUpdate.document, "update", FileLoader.PRIORITY_HIGH, 1);
showProgress(true);
}
} else if (appUpdate.url != null) {
Browser.openUrl(getContext(), appUpdate.url);
}
});

acceptTextView = new TextView(context);
Expand Down Expand Up @@ -292,6 +281,7 @@ public void show(int account, TLRPC.TL_help_appUpdate update, boolean check) {
}
SpannableStringBuilder builder = new SpannableStringBuilder(update.text);
MessageObject.addEntitiesToText(builder, update.entities, false, false, false, false);
MessageObject.replaceAnimatedEmoji(builder, update.entities, textView.getPaint().getFontMetricsInt());
textView.setText(builder);
if (update.document instanceof TLRPC.TL_document) {
acceptTextView.setText(LocaleController.getString("Update", R.string.Update) + String.format(Locale.US, " (%1$s)", AndroidUtilities.formatFileSize(update.document.size)));
Expand All @@ -301,20 +291,10 @@ public void show(int account, TLRPC.TL_help_appUpdate update, boolean check) {
NotificationCenter.getInstance(accountNum).addObserver(this, NotificationCenter.fileLoaded);
NotificationCenter.getInstance(accountNum).addObserver(this, NotificationCenter.fileLoadFailed);
NotificationCenter.getInstance(accountNum).addObserver(this, NotificationCenter.fileLoadProgressChanged);
if (check && ApplicationLoader.isStandaloneBuild()) {
TLRPC.TL_help_getAppUpdate req = new TLRPC.TL_help_getAppUpdate();
try {
req.source = ApplicationLoader.applicationContext.getPackageManager().getInstallerPackageName(ApplicationLoader.applicationContext.getPackageName());
} catch (Exception ignore) {

}
if (req.source == null) {
req.source = "";
}
ConnectionsManager.getInstance(accountNum).sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
if (response instanceof TLRPC.TL_help_appUpdate) {
final TLRPC.TL_help_appUpdate res = (TLRPC.TL_help_appUpdate) response;
if (!res.can_not_skip) {
if (check) {
UpdateHelper.getInstance().checkNewVersionAvailable((response, error) -> AndroidUtilities.runOnUIThread(() -> {
if (response != null) {
if (!response.can_not_skip) {
setVisibility(GONE);
SharedConfig.pendingAppUpdate = null;
SharedConfig.saveConfig();
Expand Down
Loading

0 comments on commit 91f289c

Please sign in to comment.