Search in sources :

Example 1 with VersionCheckClient

use of com.biglybt.core.versioncheck.VersionCheckClient in project BiglyBT by BiglySoftware.

the class PluginUpdatePlugin method initComplete.

protected void initComplete(final PluginConfig plugin_config) {
    UpdateManager update_manager = plugin_interface.getUpdateManager();
    update_manager.addListener(new UpdateManagerListener() {

        @Override
        public void checkInstanceCreated(UpdateCheckInstance inst) {
            SFPluginDetailsLoaderFactory.getSingleton().reset();
        }
    });
    final PluginManager plugin_manager = plugin_interface.getPluginManager();
    PluginInterface[] plugins = plugin_manager.getPlugins();
    int mandatory_count = 0;
    int non_mandatory_count = 0;
    for (int i = 0; i < plugins.length; i++) {
        PluginInterface pi = plugins[i];
        boolean pi_mandatory = pi.getPluginState().isMandatory();
        if (pi_mandatory) {
            mandatory_count++;
        } else {
            non_mandatory_count++;
        }
    }
    final int f_non_mandatory_count = non_mandatory_count;
    final int f_mandatory_count = mandatory_count;
    update_manager.registerUpdatableComponent(new UpdatableComponent() {

        @Override
        public String getName() {
            return ("Non-mandatory plugins");
        }

        @Override
        public int getMaximumCheckTime() {
            return (f_non_mandatory_count * ((RD_SIZE_RETRIES * RD_SIZE_TIMEOUT) / 1000));
        }

        @Override
        public void checkForUpdate(UpdateChecker checker) {
            if (checkForUpdateSupport(checker, null, false) == 0) {
                VersionCheckClient vc = VersionCheckClient.getSingleton();
                String[] rps = vc.getRecommendedPlugins();
                boolean found_one = false;
                for (int i = 0; i < rps.length; i++) {
                    String rp_id = rps[i];
                    if (plugin_manager.getPluginInterfaceByID(rp_id, false) != null) {
                        continue;
                    }
                    final String config_key = "recommended.processed." + rp_id;
                    if (!plugin_config.getPluginBooleanParameter(config_key, false)) {
                        try {
                            final PluginInstaller installer = plugin_interface.getPluginManager().getPluginInstaller();
                            StandardPlugin[] sps = installer.getStandardPlugins();
                            for (int j = 0; j < sps.length; j++) {
                                final StandardPlugin sp = sps[j];
                                if (sp.getId().equals(rp_id)) {
                                    found_one = true;
                                    checker.getCheckInstance().addListener(new UpdateCheckInstanceListener() {

                                        @Override
                                        public void cancelled(UpdateCheckInstance instance) {
                                        }

                                        @Override
                                        public void complete(UpdateCheckInstance instance) {
                                            if (instance.getUpdates().length == 0) {
                                                installRecommendedPlugin(installer, sp);
                                                plugin_config.setPluginParameter(config_key, true);
                                            }
                                        }
                                    });
                                    break;
                                }
                            }
                        } catch (Throwable e) {
                        }
                    }
                    if (found_one) {
                        break;
                    }
                }
                if (!found_one) {
                    Set<String> auto_install = vc.getAutoInstallPluginIDs();
                    final List<String> to_do = new ArrayList<>();
                    for (String pid : auto_install) {
                        if (plugin_manager.getPluginInterfaceByID(pid, false) == null) {
                            to_do.add(pid);
                        }
                    }
                    if (to_do.size() > 0) {
                        new AEThread2("pup:autoinst") {

                            @Override
                            public void run() {
                                try {
                                    Thread.sleep(120 * 1000);
                                } catch (Throwable e) {
                                    Debug.out(e);
                                    return;
                                }
                                UpdateManager update_manager = plugin_interface.getUpdateManager();
                                final List<UpdateCheckInstance> l_instances = new ArrayList<>();
                                update_manager.addListener(new UpdateManagerListener() {

                                    @Override
                                    public void checkInstanceCreated(UpdateCheckInstance instance) {
                                        synchronized (l_instances) {
                                            l_instances.add(instance);
                                        }
                                    }
                                });
                                UpdateCheckInstance[] instances = update_manager.getCheckInstances();
                                l_instances.addAll(Arrays.asList(instances));
                                long start = SystemTime.getMonotonousTime();
                                while (true) {
                                    if (SystemTime.getMonotonousTime() - start >= 5 * 60 * 1000) {
                                        break;
                                    }
                                    try {
                                        Thread.sleep(5000);
                                    } catch (Throwable e) {
                                        Debug.out(e);
                                        return;
                                    }
                                    if (l_instances.size() > 0) {
                                        boolean all_done = true;
                                        for (UpdateCheckInstance instance : l_instances) {
                                            if (!instance.isCompleteOrCancelled()) {
                                                all_done = false;
                                                break;
                                            }
                                        }
                                        if (all_done) {
                                            break;
                                        }
                                    }
                                }
                                if (update_manager.getInstallers().length > 0) {
                                    return;
                                }
                                PluginInstaller installer = plugin_interface.getPluginManager().getPluginInstaller();
                                List<InstallablePlugin> sps = new ArrayList<>();
                                for (String pid : to_do) {
                                    try {
                                        StandardPlugin sp = installer.getStandardPlugin(pid);
                                        if (sp != null) {
                                            log.log("Auto-installing " + pid);
                                            sps.add(sp);
                                        } else {
                                            log.log("Standard plugin '" + pid + "' missing");
                                        }
                                    } catch (Throwable e) {
                                        log.log("Standard plugin '" + pid + "' missing", e);
                                    }
                                }
                                if (sps.size() > 0) {
                                    Map<Integer, Object> properties = new HashMap<>();
                                    properties.put(UpdateCheckInstance.PT_UI_STYLE, UpdateCheckInstance.PT_UI_STYLE_NONE);
                                    properties.put(UpdateCheckInstance.PT_UI_DISABLE_ON_SUCCESS_SLIDEY, true);
                                    try {
                                        installer.install(sps.toArray(new InstallablePlugin[sps.size()]), false, properties, new PluginInstallationListener() {

                                            @Override
                                            public void completed() {
                                            }

                                            @Override
                                            public void cancelled() {
                                            }

                                            @Override
                                            public void failed(PluginException e) {
                                            }
                                        });
                                    } catch (Throwable e) {
                                        log.log("Auto install failed", e);
                                    }
                                }
                            }
                        }.start();
                    }
                }
            }
        }
    }, false);
    update_manager.registerUpdatableComponent(new UpdatableComponent() {

        @Override
        public String getName() {
            return ("Mandatory plugins");
        }

        @Override
        public int getMaximumCheckTime() {
            return (f_mandatory_count * ((RD_SIZE_RETRIES * RD_SIZE_TIMEOUT) / 1000));
        }

        @Override
        public void checkForUpdate(UpdateChecker checker) {
            checkForUpdateSupport(checker, null, true);
        }
    }, true);
    update_manager.addListener(new UpdateManagerListener() {

        @Override
        public void checkInstanceCreated(UpdateCheckInstance instance) {
            log.log(LoggerChannel.LT_INFORMATION, "**** Update check starts ****");
        }
    });
}
Also used : PluginInstallationListener(com.biglybt.pif.installer.PluginInstallationListener) StandardPlugin(com.biglybt.pif.installer.StandardPlugin) InstallablePlugin(com.biglybt.pif.installer.InstallablePlugin) PluginInstaller(com.biglybt.pif.installer.PluginInstaller) VersionCheckClient(com.biglybt.core.versioncheck.VersionCheckClient)

Aggregations

VersionCheckClient (com.biglybt.core.versioncheck.VersionCheckClient)1 InstallablePlugin (com.biglybt.pif.installer.InstallablePlugin)1 PluginInstallationListener (com.biglybt.pif.installer.PluginInstallationListener)1 PluginInstaller (com.biglybt.pif.installer.PluginInstaller)1 StandardPlugin (com.biglybt.pif.installer.StandardPlugin)1