• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

opensrp / opensrp-client-core / #180

05 Aug 2024 11:55AM UTC coverage: 68.398% (+0.003%) from 68.395%
#180

Pull #925

github

web-flow
Merge dae4bbe8e into 4fcf06cf5
Pull Request #925: migrate SyncserviceJob to work manager

18313 of 26774 relevant lines covered (68.4%)

0.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.59
opensrp-core/src/main/java/org/smartregister/view/activity/DrishtiApplication.java
1
package org.smartregister.view.activity;
2

3
import static org.smartregister.util.Log.logError;
4

5
import android.app.Application;
6
import android.os.Build;
7

8
import androidx.annotation.NonNull;
9
import androidx.annotation.Nullable;
10
import androidx.multidex.MultiDex;
11

12
import net.sqlcipher.database.SQLiteDatabase;
13

14
import org.json.JSONObject;
15
import org.smartregister.BuildConfig;
16
import org.smartregister.Context;
17
import org.smartregister.CoreLibrary;
18
import org.smartregister.R;
19
import org.smartregister.repository.DrishtiRepository;
20
import org.smartregister.repository.Repository;
21
import org.smartregister.sync.ClientProcessorForJava;
22
import org.smartregister.sync.P2PClassifier;
23
import org.smartregister.util.CrashLyticsTree;
24
import org.smartregister.util.CredentialsHelper;
25
import org.smartregister.util.OpenSRPImageLoader;
26

27
import java.io.File;
28
import java.util.ArrayList;
29
import java.util.Locale;
30

31
import timber.log.Timber;
32

33
public abstract class DrishtiApplication extends Application {
1✔
34

35
    protected static DrishtiApplication mInstance;
36
    private static OpenSRPImageLoader cachedImageLoader;
37
    private static CredentialsHelper credentialsHelper;
38
    protected Locale locale = null;
1✔
39
    protected Context context;
40
    protected Repository repository;
41
    private byte[] password;
42
    private String username;
43

44
    public static synchronized <X extends DrishtiApplication> X getInstance() {
45
        return (X) mInstance;
1✔
46
    }
47

48
    public static String getAppDir() {
49
        File appDir = DrishtiApplication.getInstance().getApplicationContext()
1✔
50
                .getDir("opensrp", android.content.Context.MODE_PRIVATE); //Creating an internal
1✔
51
        // dir;
52
        return appDir.getAbsolutePath();
1✔
53
    }
54

55
    public static OpenSRPImageLoader getCachedImageLoaderInstance() {
56
        if (cachedImageLoader == null) {
1✔
57
            cachedImageLoader = new OpenSRPImageLoader(
1✔
58
                    DrishtiApplication.getInstance().getApplicationContext(),
1✔
59
                    R.drawable.woman_placeholder).setFadeInImage((Build.VERSION.SDK_INT >= 12));
1✔
60
        }
61

62
        return cachedImageLoader;
1✔
63
    }
64

65
    @Nullable
66
    public P2PClassifier<JSONObject> getP2PClassifier() {
67
        return null;
×
68
    }
69

70
    @Override
71
    public void onCreate() {
72
        try {
73
            super.onCreate();
1✔
74

75
            initializeCrashLyticsTree();
1✔
76
            mInstance = this;
1✔
77
            SQLiteDatabase.loadLibs(this);
1✔
78
        } catch (UnsatisfiedLinkError e) {
×
79
            logError("Error on onCreate: " + e);
×
80
        }
1✔
81
    }
1✔
82

83
    /**
84
     * Plant the crashlytics tree fro every application to use
85
     */
86
    public void initializeCrashLyticsTree() {
87
        if (BuildConfig.DEBUG) {
1✔
88
            Timber.plant(new Timber.DebugTree());
1✔
89
        } else {
90
            Timber.plant(new CrashLyticsTree());
×
91
        }
92
    }
1✔
93

94
    public abstract void logoutCurrentUser();
95

96
    @Override
97
    protected void attachBaseContext(android.content.Context base) {
98
        super.attachBaseContext(base);
1✔
99
        MultiDex.install(this);
1✔
100
    }
1✔
101

102
    public Repository getRepository() {
103
        ArrayList<DrishtiRepository> drishtiRepositoryList = CoreLibrary.getInstance().context().sharedRepositories();
1✔
104
        DrishtiRepository[] drishtiRepositoryArray = drishtiRepositoryList.toArray(new DrishtiRepository[drishtiRepositoryList.size()]);
1✔
105
        if (repository == null) {
1✔
106
            repository = new Repository(getInstance().getApplicationContext(), null, drishtiRepositoryArray);
1✔
107
        }
108
        return repository;
1✔
109
    }
110

111
    public CredentialsHelper credentialsProvider() {
112

113
        if (credentialsHelper == null) {
1✔
114
            credentialsHelper = new CredentialsHelper(context);
1✔
115
        }
116

117
        return credentialsHelper;
1✔
118
    }
119

120
    public final byte[] getPassword() {
121

122
        if (password == null) {
1✔
123
            password = credentialsProvider().getCredentials(getUsername(), CredentialsHelper.CREDENTIALS_TYPE.DB_AUTH);
1✔
124
        }
125

126
        return password;
1✔
127
    }
128

129
    public void setPassword(byte[] password) {
130
        this.password = password;
1✔
131
    }
1✔
132

133
    @NonNull
134
    public ClientProcessorForJava getClientProcessor() {
135
        return ClientProcessorForJava.getInstance(context.applicationContext());
1✔
136
    }
137

138
    public String getUsername() {
139
        if (username == null) {
1✔
140
            username = context.userService().getAllSharedPreferences().fetchRegisteredANM();
1✔
141
        }
142
        return username;
1✔
143
    }
144

145
    @Override
146
    public void onTerminate() {
147
        closePendingTransactions();
1✔
148
        super.onTerminate();
1✔
149
    }
1✔
150

151
    private void closePendingTransactions() {
152
        if (repository != null && repository.getWritableDatabase().isOpen()
1✔
153
                && repository.getWritableDatabase().inTransaction()) {
1✔
154
            Timber.e(new RuntimeException("Application closed while transactions are in progress. Data maybe lost"));
1✔
155
            repository.getWritableDatabase().endTransaction();
1✔
156
            context.allSharedPreferences().updateTransactionsKilledFlag(true);
1✔
157
        }
158
    }
1✔
159

160
    public Context getContext() {
161
        return context;
1✔
162
    }
163

164
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc