aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/Application.java
blob: e556370601e9fef2f34f84df48160f9b1e9e3837 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2012-2014 lacolaco.net
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package net.lacolaco.smileessence;

import com.crashlytics.android.Crashlytics;
import com.crashlytics.android.core.CrashlyticsCore;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import io.fabric.sdk.android.Fabric;
import net.lacolaco.smileessence.data.Account;
import net.lacolaco.smileessence.data.OrmaHolder;
import net.lacolaco.smileessence.logging.Logger;
import net.lacolaco.smileessence.preference.UserPreferenceHelper;
import net.lacolaco.smileessence.util.UIHandler;

import java.util.Collections;
import java.util.Set;
import java.util.WeakHashMap;

/**
 * プロセスと同期しているオブジェクト
 * 現在のテーマのリソース ID と現在のアカウント(およびアカウント変更イベントリスナー)を保持します
 * MainActivity の onCreate で resetState を呼び、保持しているデータを破棄すること
 */
public class Application extends android.app.Application {
    private static Application instance;
    private Account currentAccount;
    private final Set<OnCurrentAccountChangedListener> currentAccountChangedListeners = Collections.newSetFromMap(new WeakHashMap<>());
    private int resId;
    private RefWatcher refWatcher;

    @Override
    public void onCreate() {
        super.onCreate();
        Fabric.with(this, new Crashlytics.Builder().core(new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build()).build());
        OrmaHolder.initialize(this);
        instance = this; // プロセスの寿命の間 1 度しか呼ばれないので安全
        refWatcher = LeakCanary.install(this);
        Logger.debug("onCreate");
    }

    // --------------------- reset ---------------------
    public void resetState() {
        currentAccount = null;
        currentAccountChangedListeners.clear();
        resId = -1;
    }

    // --------------------- get instance ---------------------

    public static Application getInstance() {
        Application obj = instance;
        if (obj == null) {
            throw new IllegalStateException("[BUG] Application is not initialized?");
        } else {
            return obj;
        }
    }

    // --------------------- theme ---------------------
    public int getThemeResId() {
        if (resId == -1) {
            Logger.debug("setting theme index: " + UserPreferenceHelper.getInstance().getThemeIndex());
            resId = UserPreferenceHelper.getInstance().getThemeIndex() == 0 ? R.style.theme_dark : R.style.theme_light;
        }
        return resId;
    }

    // --------------------- current account ---------------------
    public Account getCurrentAccount() {
        return currentAccount;
    }

    public void setCurrentAccount(Account val) {
        Logger.debug(String.format("setCurrentAccount: %s", val.getUser().getScreenName()));
        currentAccount = val;
        for (OnCurrentAccountChangedListener listener : currentAccountChangedListeners) {
            new UIHandler().post(() -> listener.onCurrentAccountChanged(val));
        }
    }

    public void addOnCurrentAccountChangedListener(OnCurrentAccountChangedListener listener) {
        currentAccountChangedListeners.add(listener);
    }

    public interface OnCurrentAccountChangedListener {
        void onCurrentAccountChanged(Account newAccount);
    }

    // --------------------- LeakCanary ---------------------
    public RefWatcher getRefWatcher() {
        return refWatcher;
    }
}