• Stars
    star
    272
  • Rank 151,235 (Top 3 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created about 9 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Init helps app schedule complex task-group like initialization with type, priority and multi-process.

Init Maven Central

Init helps app schedule complex task-group like initialization with type, priority and multi-process (you know that every process will run application's onCreate), thus improves efficiency.

It is originally designed for application initialization, but not confined to that, and can be applied to any complex task-group(like initialization procedure).

The library does not depend on any third-party library, it depends on Android in the case of Context and Log, and mostly depends on Java concurrent.

It's named Init for the sake of its original inspiration.

See Wiki for detailed usage.

For Chinese 中文戳这里

How

The task-group(like initialization procedure) can be abstracted to flow, wave and task.

flow

Flow is a coarse-grained concept, normally we have only one flow, but under certain condition, we may have several flow like patch flow, fake UI flow(to make user feel faster), broadcast flow, etc.

Each wave can be started only when all blocked task in last wave finished, and all tasks belongs tothe wave will started at the same time(if no delay set).

As for task, they can be divided into two types

  1. Blocked task, blue tasks in the picture.
  2. Asynchronous task, can be
  • completely asynchronous or across several waves like the green task.
  • asynchronous task chain, like the two red tasks.

Usage

dependencies {
    compile 'cn.zhaiyifan:init:1.0.1'
}
public class DemoApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        // The library needs application context to get process information.
        Init.init(this);
        // Init.init(this, logProxy) enables custom log component
        
        Task task1 = new Task("task1") {

            @Override
            protected void start() {
                doSomeThing();
            }

            // when runs on process which makes the method return true
            @Override
            public boolean runOnProcess(String processName) {
                return processName.equals("cn.zhaiyifan.demo");
            }
        };
        
        // Create a task which is not blocked with 300 milliseconds delay.
        Task task2 = new Task("task2", false, 300) {

            @Override
            protected void start() {
                doSomeThing();
            }
        };

        // Create more tasks named task3, task4, task5, etc.
        ...

        // This is like the two red tasks in the flow picture, task5 depends on task4.
        task5.setParentTask(task4);

        Flow flow = new Flow("flow");
        // Add task1 and task2 to wave1
        flow.addTask(1, task1)
                .addTask(1, task2)
                // Add task3 and task4 to wave2
                .addTask(2, task3)
                .addTask(2, task4)
                // Add task5 and task4 to wave3, task5 can be started only after task4 finished
                .addTask(3, task5);

        Init.start(flow);
    }

Let's have a look at log, we can see that the initialization which may take up to 2700ms only run 1307ms now.

10-04 18:53:54.789 646-666/cn.zhaiyifan.init I/Task: task2 runs 500
10-04 18:53:55.289 646-665/cn.zhaiyifan.init I/Task: task1 runs 1000
10-04 18:53:55.591 646-741/cn.zhaiyifan.init I/Task: task3 runs 300
10-04 18:53:55.592 646-646/cn.zhaiyifan.init I/Flow: flow runs 1307
10-04 18:53:55.990 646-740/cn.zhaiyifan.init I/Task: task4 runs 700
10-04 18:53:56.191 646-783/cn.zhaiyifan.init I/Task: task5 runs 200

See demo project for more details.

Useful api:

// to set thread pool size
Init.setThreadPoolSize(...)

// cancel a started flow
Init.cancel(...)

// get flow status
Init.getFlowStatus(...)

// get specific task status
flow.getTaskStatus(taskName)

// set timeout, may not be useful for application init, but can be used for other init operation
flow.setTimeout(5000)

etc.

Why this

Imagine how we initialize a large application like Facebook, QQ, Wechat, etc, we will face sth like:

public class XXXApplication {

    // for multi-dex apps
    @Override
    protected void attachBaseContext(Context base) {
        // log init
        ...
        // eventbus init...
        ...
        // global variables init
        ...
        // process related
        String processName = ...
        boolean isMainProcess = ...
        ProcessInit.attachBaseContext(this, processName, isMainProcess);
    }

    @Override
    protected void onCreate() {
        // process related
        String processName = ...
        boolean isMainProcess = ...

        // CrashHandler, SafeMode, plugin, image manager, database, download, update, etc init

        ProcessInit.onCreate(this, processName, isMainProcess);
    }

}

public class ProcessInit {
    ...
    public static void onCreate(Application application, boolean isMainProcess, String processName) {
        if (isMainProcess) {

        }
    } else if (processName.contains(PUSH_PROCESS_SUFFIX)) {
        ...
    } else if (processName.contains(WEB_PROCESS_SUFFIX)) {
        ...
    } else if (processName.contains(MUSIC_PROCESS_SUFFIX)) {
        ...
    } else {
        ...
    }
    ...
}

You see how complicated the initialization can be when the application grows, some operation should be after the other, and some can be done parallel, and some... Then we need to change the implementation of every init, to make some asynchronous, which makes code dirty and hard to understand.

How to make it simpler? I came up with this library.

Roadmap

  • 1.0 October - A workable solution to principles mentioned above DONE
  • 1.1 In this year - Support more complex init flow WIP
  • 2.0 Ability to reverse initialization code using this library to flow picture.

Contribute

Contribution is welcomed, you can create an issue or directly make a pull request.

More Repositories

1

AndroidPerformanceMonitor

A transparent ui-block detection library for Android. (known as BlockCanary)
Java
6,572
star
2

LyricHere

Local lyric-play support music player, includes a powerful LyricView
Java
329
star
3

LyricView

Android LyricView that accepts lrc stream as input, A powerful lyric parser is also contained.
Java
264
star
4

DataBindingAdapter

Super simple RecyclerView adapter using Data Binding Technology.
Java
245
star
5

RememberEditText

An EditText which can remember last several input, free developer from managing cache everywhere themselves. !! NOT ViewGroup !!
Java
163
star
6

DataBindingSample

Android Data Binding Sample
Java
156
star
7

InterestingTitleBar

TitleBar (or custom ActionBar) with immersive status bar and scroll animation like tumblr app (icon and text color gradually change to end color).
Java
111
star
8

react-native-preloader

React Native Pre-loader for Android.
Java
53
star
9

ResourceOverlayDemo

Demo to show Android aapt overlay and gradle resource merge
Java
23
star
10

AndroidProjectFrom0

Repository to show the best practice from the series blog Android-Project-From-0
Java
20
star
11

GoWebServer

A web server use Go language, includes authentication.
Go
10
star
12

markzhai.github.io

Mark Zhai's hexo homepage
HTML
10
star
13

nevo-direct-reply

Decorators for nevolution which provides direct reply experience on wechat, whatsapp, etc.
Java
9
star
14

GitHubExplorer

(wip) An github android client with explore and trending feature.
Java
8
star
15

ErogameScape-Mining

A spider and data-mining project on ErogameScape
Python
5
star
16

PlaneWar

A clone of initial version of 飞机大战 from 微信. SurfaceView implementation
Java
4
star
17

add-fitsSystemWindows

add fitsSystemWindows to all layout files (use if to add Translucent Status Bar support easily)
Python
3
star
18

Logger

Logger is a logging tool helps unify release log and debug log, with some little advanced usage
Java
3
star
19

HelloVue

Repository for Android developer to learn Vue.js
JavaScript
3
star
20

Galaxy

Galaxy is a hybrid framework for Android.
3
star
21

dimBody

Screen dimming according to the barrier in front of the screen for CSIT6910 HKUST.
C++
1
star
22

blog-comment

blog comment
1
star
23

MusicPlayer

A Music player implemented by Java and Processing, it has wonderful music visualization and rhythm game for any music.
Java
1
star