CheckVersionLib Documentation

repository·master·Indexed 25 days ago

https://github.com/sunofbeach2/checkversionlib

An Android library for version checking and update management. It supports chain programming, custom HTTP request parameters via requestVersion(), and a downloadOnly() mode for direct updates. The library provides extensive extensibility for customizing version, downloading, and failure dialogs, as well as listeners for monitoring APK download progress and lifecycle.

Tokens
5.7K
Snippets
13
Records
27
Agent score
81%

What's inside CheckVersionLib

  1. Customize the Update Dialog UI

    master

    You can provide your own Dialog implementation by setting a CustomVersionDialogListener.

    Requirements:

    • Use the context provided in the callback to instantiate your dialog.
    • The dialog must contain a 'Confirm Download' button with the ID @id/versionchecklib_version_dialog_commit.
    • If you include a 'Cancel' button, it must have the ID @id/versionchecklib_version_dialog_cancel.
    • Use the UIData (passed as versionBundle) to retrieve custom data you set during the request phase.
    builder.setCustomVersionDialogListener((context, versionBundle) -> {
        BaseDialog baseDialog = new BaseDialog(context, R.style.BaseDialog, R.layout.custom_dialog_one_layout);
        TextView textView = baseDialog.findViewById(R.id.tv_msg);
        textView.setText(versionBundle.getContent());
        return baseDialog;
    });
  2. Customize the Download Failed UI

    master

    Implement CustomDownloadFailedListener to show a custom error dialog.

    Requirements:

    • If you include a 'Retry' button, its ID must be @id/versionchecklib_failed_dialog_retry.
    • If you include 'Confirm/Cancel' buttons, their ID must be @id/versionchecklib_failed_dialog_cancel.
  3. Customize the update UI

    master

    To create a custom UI for the update dialog:

    1. Create an Activity that extends VersionDialogActivity.
    2. Set the Activity's theme to transparent: android:theme="@style/versionCheckLibvtransparentTheme".
    3. Set the launch mode to singleTask: android:launchMode="singleTask".
    4. Register your custom class in VersionParams using setCustomDownloadActivityClass(YourCustomActivity.class).

    Key Methods for Customization:

    • getVersionTitle(): Retrieves the title passed from the service.
    • getVersionUpdateMsg(): Retrieves the update message passed from the service.
    • getVersionParamBundle(): Retrieves the extra parameters passed from the service.
    • showVersionDialog(): Override this to implement your own dialog logic. Crucial: In your confirmation button's click listener, you must call super.dealAPK(); to trigger the download/install process.
    • showLoadingDialog(int currentProgress): Override to customize the progress/loading UI.
    • showFailDialog(): Override to customize the error/failure UI.
    // Inside your CustomVersionDialogActivity
    versionDialog = new BaseDialog(this, R.style.BaseDialog, R.layout.custom_dialog_two_layout);
    TextView tvTitle = (TextView) versionDialog.findViewById(R.id.tv_title);
    TextView tvMsg = (TextView) versionDialog.findViewById(R.id.tv_msg);
    Button btnUpdate = (Button) versionDialog.findViewById(R.id.btn_update);
    
    versionDialog.show();
    versionDialog.setOnDismissListener(this);
    
    tvTitle.setText(getVersionTitle());
    tvMsg.setText(getVersionUpdateMsg());
    
    btnUpdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            versionDialog.dismiss();
            // MUST call super.dealAPK() to proceed with download
            CustomVersionDialogActivity.super.dealAPK();
        }
    });
  4. Customize the Downloading Progress UI

    master

    To customize the UI shown while the APK is downloading, implement CustomDownloadingDialogListener. This provides two methods:

    • getCustomDownloadingDialog(Context context, int progress, UIData versionBundle): Returns the initial dialog.
    • updateUI(Dialog dialog, int progress, UIData versionBundle): Called continuously to update progress bars or text.

    Requirements:

    • If your custom dialog includes a cancel action, the button ID must be @id/versionchecklib_loading_dialog_cancel.
    builder.setCustomDownloadingDialogListener(new CustomDownloadingDialogListener() {
        @Override
        public Dialog getCustomDownloadingDialog(Context context, int progress, UIData versionBundle) {
            return new BaseDialog(context, R.style.BaseDialog, R.layout.custom_download_layout);
        }
    
        @Override
        public void updateUI(Dialog dialog, int progress, UIData versionBundle) {
            TextView tvProgress = dialog.findViewById(R.id.tv_progress);
            ProgressBar progressBar = dialog.findViewById(R.id.pb);
            progressBar.setProgress(progress);
            tvProgress.setText(getString(R.string.versionchecklib_progress, progress));
        }
    });
  5. Implement version checking with a custom service

    master

    To perform a full version check (requesting an API and showing a dialog), follow these steps:

    1. Create a custom service: Extend AVersionService and implement onResponses(AVersionService service, String response). This method handles the API response. You must parse the data and decide whether to show the update dialog using service.showVersionDialog().

    2. Start the check: Use AllenChecker.startVersionCheck with a VersionParams.Builder configured with your request URL and the custom service class.

    Example implementation of the service logic:

    if (serverVersion > clientVersion) { 
        // Pass download URL, title, and update message
        service.showVersionDialog(downloadUrl, title, updateMsg);
        // Or with a bundle for extra parameters
        // service.showVersionDialog(downloadUrl, title, updateMsg, bundle);
    }
    VersionParams.Builder builder = new VersionParams.Builder()
                 .setRequestUrl("http://www.baidu.com")
                 .setService(DemoService.class);
                 
    AllenChecker.startVersionCheck(this, builder.build());
  6. Implement Force Update logic

    master

    To implement a 'Force Update' (where the user cannot dismiss the dialog to continue using the app), you should listen to the dialog dismissal and download events in your custom Activity by implementing:

    • setApkDownloadListener(this)
    • setDialogDimissListener(this)

    When the user dismisses the dialog, you can use the dialogDismiss callback to close the application.

  7. Install CheckVersionLib via JitPack

    master

    To use CheckVersionLib in your Android project, add the JitPack repository to your allprojects block and then add the dependency to your dependencies block.

    allprojects {
    	repositories {
    		...
    		maven { url 'https://jitpack.io' }
    	}
    }
    
    dependencies {
    	implementation 'com.github.AlexLiuSheng:CheckVersionLib:2.4.2'
    }
  8. Customize the version dialog UI

    master

    To use a custom UI instead of the default dialogs, follow these steps:

    1. Create an Activity that extends VersionDialogActivity.
    2. Set the Activity's theme to transparent in your AndroidManifest.xml: android:theme="@style/versionCheckLibvtransparentTheme".
    3. Pass your custom class to the service using setCustomDownloadActivityClass(CustomVersionDialogActivity.class) within your VersionParams configuration.

    Customization Hooks:

    • Version Dialog: Override showVersionDialog() and implement your own logic. Use downloadFile(url) or downloadFile(url, filecallback) to trigger downloads. Do not call the superclass method.
    • Loading Dialog: Override showLoadingDialog(int currentProgress) to implement a custom progress UI.
    • Failure Dialog: Override showFailDialog to implement custom error handling.
    • Listeners: You can implement the following listeners within your custom Activity:
      • setOnDownloadSuccessListener(this)
      • setCommitClickListener(this)
      • setCancelClickListener(this)
      • setOnDownloadingListener(this)
  9. ProGuard configuration for CheckVersionLib

    master

    Add the following rules to your ProGuard configuration to ensure the library and its dependencies (like EventBus) work correctly:

    -keepattributes *Annotation*
    -keepclassmembers class * {
        @org.greenrobot.eventbus.Subscribe <methods>;
    }
    -keep enum org.greenrobot.eventbus.ThreadMode { *; }
    
    # Only required if you use AsyncExecutor
    -keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
        <init>(java.lang.Throwable);
    }
    -keep class com.allenliu.versionchecklib.**{*;}
  10. ProGuard Configuration

    master

    If you use ProGuard/R8, add the following rules to prevent the library and its dependencies (like EventBus) from being stripped or obfuscated incorrectly. Note that if you use AsyncExecutor, additional rules for ThrowableFailureEvent are required.

    -keepattributes *Annotation*
    -keepclassmembers class * {
        @org.greenrobot.eventbus.Subscribe <methods>;
    }
    -keep enum org.greenrobot.eventbus.ThreadMode { *; }
    
    # Only required if you use AsyncExecutor
    -keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
        <init>(java.lang.Throwable);
    }
  11. Prevent memory leaks by canceling missions

    master

    To avoid memory leaks, cancel all active missions when the library is no longer needed (e.g., in onDestroy()).

    • Cancel all missions: AllenVersionChecker.getInstance().cancelAllMission();
    • Cancel missions for a specific context: AllenVersionChecker.getInstance().cancelAllMission(this);