Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ChannelSearchProvider.java « channel « mumla « lublin « se « java « main « src « app - gitlab.com/quite/mumla.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3b2d17042679e8c41a0f89db19c6da33fab292a2 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
 * Copyright (C) 2014 Andrew Comminos
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package se.lublin.mumla.channel;

import android.app.SearchManager;
import android.content.ComponentName;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.IBinder;
import android.util.Log;

import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

import se.lublin.humla.IHumlaService;
import se.lublin.humla.IHumlaSession;
import se.lublin.humla.model.IChannel;
import se.lublin.humla.model.IUser;
import se.lublin.mumla.Constants;
import se.lublin.mumla.R;
import se.lublin.mumla.service.MumlaService;

public class ChannelSearchProvider extends ContentProvider {
	
	public static final String INTENT_DATA_CHANNEL = "channel";
	public static final String INTENT_DATA_USER = "user";

    private IHumlaService mService;
    private final Object mServiceLock = new Object();

	private ServiceConnection mConn = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			mService = ((MumlaService.MumlaBinder) service).getService();
            synchronized (mServiceLock) {
                mServiceLock.notify();
            }
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			mService = null;
		}
	};
	
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean onCreate() {
		return true;
	}
	

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {	
		
		// Try to connect to the service. Wait for conn to establish.
		if(mService == null) {
			Intent serviceIntent = new Intent(getContext(), MumlaService.class);
			getContext().bindService(serviceIntent, mConn, 0);

            synchronized (mServiceLock) {
                try {
                    mServiceLock.wait(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                if(mService == null) {
                    Log.v(Constants.TAG, "Failed to connect to service from search provider!");
                    return null;
                }
            }
		}

        if (!mService.isConnected())
            return null;

        IHumlaSession session = mService.getSession();
		
		String query = "";
		for(int x=0;x<selectionArgs.length;x++) {
			query += selectionArgs[x];
			if(x != selectionArgs.length-1)
				query += " ";
		}
		
		query = query.toLowerCase(Locale.getDefault());
		
		MatrixCursor cursor = new MatrixCursor(new String[] { "_ID", SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA, SearchManager.SUGGEST_COLUMN_TEXT_1, SearchManager.SUGGEST_COLUMN_ICON_1, SearchManager.SUGGEST_COLUMN_TEXT_2, SearchManager.SUGGEST_COLUMN_INTENT_DATA });

        List<IChannel> channels = channelSearch(session.getRootChannel(), query);
        List<IUser> users = userSearch(session.getRootChannel(), query);

        for(int x=0;x<channels.size();x++) {
            IChannel channel = channels.get(x);
            cursor.addRow(new Object[] { x, INTENT_DATA_CHANNEL, channel.getName(), R.drawable.ic_action_channels, getContext().getResources().getQuantityString(R.plurals.search_channel_users, channel.getSubchannelUserCount(), channel.getSubchannelUserCount()), channel.getId() });
        }

        for(int x=0;x<users.size();x++) {
            IUser user = users.get(x);
            cursor.addRow(new Object[] { x, INTENT_DATA_USER, user.getName(), R.drawable.ic_action_user_dark, getContext().getString(R.string.user), user.getSession() });
        }
		return cursor;
	}

    /**
     * Recursively searches the channel tree for a user with a name containing the given string,
     * ignoring case.
     * @param root The channel to recursively search for users within.
     * @param str The string to match against the user's name. Case insensitive.
     * @return A list of users whose names contain str.
     */
    private List<IUser> userSearch(IChannel root, String str) {
        List<IUser> list = new LinkedList<IUser>();
        userSearch(root, str, list);
        return list;
    }

    /**
     * @see #userSearch(IChannel,String)
     */
    private void userSearch(IChannel root, String str, List<IUser> users) {
        if (root == null) {
            return;
        }
        for (IUser user : root.getUsers()) {
            if (user != null && user.getName() != null
                    && user.getName().toLowerCase().contains(str.toLowerCase())) {
                users.add(user);
            }
        }
        for (IChannel subc : root.getSubchannels()) {
            if (subc != null)
                userSearch(subc, str, users);
        }
    }

    /**
     * Recursively searches the channel tree for a channel with a name containing the given string,
     * ignoring case.
     * @param root The channel to recursively search for subchannels within.
     * @param str The string to match against the channel's name. Case insensitive.
     * @return A list of channels whose names contain str.
     */
    private List<IChannel> channelSearch(IChannel root, String str) {
        List<IChannel> list = new LinkedList<IChannel>();
        channelSearch(root, str, list);
        return list;
    }

    /**
     * @see #channelSearch(IChannel,String)
     */
    private void channelSearch(IChannel root, String str, List<IChannel> channels) {
        if (root == null) {
            return;
        }

        if (root.getName().toLowerCase().contains(str.toLowerCase())) {
            channels.add(root);
        }

        for (IChannel subc : root.getSubchannels()) {
            if (subc != null)
                channelSearch(subc, str, channels);
        }
    }

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

}