aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/ui/suggestion/util/SuggestionServiceConnectionManager.java
blob: bcbdebe30e193656c55cbc2bf261ec9378c1d3a9 (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
package org.wordpress.android.ui.suggestion.util;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import org.wordpress.android.ui.suggestion.service.SuggestionService;

public class SuggestionServiceConnectionManager implements ServiceConnection {

    private final Context mContext;
    private final int mRemoteBlogId;
    private boolean mAttemptingToBind = false;
    private boolean mBindCalled = false;

    public SuggestionServiceConnectionManager(Context context, int remoteBlogId) {
        mContext = context;
        mRemoteBlogId = remoteBlogId;
    }

    public void bindToService() {
        if (!mAttemptingToBind) {
            mAttemptingToBind = true;
            mBindCalled = true;
            Intent intent = new Intent(mContext, SuggestionService.class);
            mContext.bindService(intent, this, Context.BIND_AUTO_CREATE);
        }
    }

    public void unbindFromService() {
        mAttemptingToBind = false;
        if (mBindCalled) {
            mContext.unbindService(this);
            mBindCalled = false;
        }
    }

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        SuggestionService.SuggestionBinder b = (SuggestionService.SuggestionBinder) iBinder;
        SuggestionService suggestionService = b.getService();

        suggestionService.updateSuggestions(mRemoteBlogId);
        suggestionService.updateTags(mRemoteBlogId);

        mAttemptingToBind = false;
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        // noop
    }
}