10000 Add base suggestion interface and data entity and repository that fet… · roychowdhuryrohit-dev/Android@22494c9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 22494c9

Browse files
committed
Add base suggestion interface and data entity and repository that fetch suggestions from the remote server
1 parent 20cbec5 commit 22494c9

File tree

6 files changed

+244
-4
lines changed

6 files changed

+244
-4
lines changed

app/src/main/java/com/duckduckgo/app/Injector.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.duckduckgo.app.data.bookmark.BookmarkJsonEntityMapper;
2222
import com.duckduckgo.app.data.bookmark.BookmarkSharedPreferences;
2323
import com.duckduckgo.app.data.bookmark.SharedPreferencesBookmarkRepository;
24+
import com.duckduckgo.app.data.suggestion.DDGSuggestionRepository;
2425
import com.duckduckgo.app.data.tab.SharedPreferencesTabRepository;
2526
import com.duckduckgo.app.data.tab.TabJsonEntityMapper;
2627
import com.duckduckgo.app.data.tab.TabSharedPreferences;
@@ -84,6 +85,14 @@ public static SharedPreferencesBookmarkRepository injectSharedPreferencesBookmar
8485
return (SharedPreferencesBookmarkRepository) instances.get(key);
8586
}
8687

88+
public static DDGSuggestionRepository injectDDGSuggestionRepository() {
89+
String key = getKeyforClass(DDGSuggestionRepository.class);
90+
if (!instances.containsKey(key)) {
91+
instances.put(key, instantiateDDGSuggestionRepository());
92+
}
93+
return (DDGSuggestionRepository) instances.get(key);
94+
}
95+
8796
private static BrowserPresenterImpl instantiateBrowserPresenterImpl() {
8897
return new BrowserPresenterImpl(injectSharedPreferencesTabRepository(), injectSharedPreferencesBookmarkRepository());
8998
}
@@ -100,6 +109,10 @@ private static SharedPreferencesBookmarkRepository instantiateSharedPreferencesB
100109
return new SharedPreferencesBookmarkRepository(injectBookmarkSharedPreferences(), instantiateBookmarkJsonEntityMapper());
101110
}
102111

112+
private static DDGSuggestionRepository instantiateDDGSuggestionRepository() {
113+
return new DDGSuggestionRepository();
114+
}
115+
103116
private static TabJsonEntityMapper instantiateTabJsonEntityMapper() {
104117
return new TabJsonEntityMapper();
105118
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (c) 2017 DuckDuckGo
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.duckduckgo.app.data.suggestion;
18+
19+
import android.support.annotation.NonNull;
20+
21+
import com.duckduckgo.app.domain.suggestion.Suggestion;
22+
import com.duckduckgo.app.domain.suggestion.SuggestionRepository;
23+
import com.duckduckgo.app.util.AppUrls;
24+
25+
import org.json.JSONArray;
26+
import org.json.JSONException;
27+
import org.json.JSONObject;
28+
29+
import java.io.BufferedReader;
30+
import java.io.IOException;
31+
import java.io.InputStreamReader;
32+
import java.net.MalformedURLException;
33+
import java.net.URL;
34+
import java.util.ArrayList;
35+
import java.util.List;
36+
37+
import javax.net.ssl.HttpsURLConnection;
38+
39+
import timber.log.Timber;
40+
41+
/**
42+
* Created by fgei on 6/30/17.
43+
*/
44+
45+
public class DDGSuggestionRepository implements SuggestionRepository {
46+
47+
@Override
48+
public List<Suggestion> getSuggestions(@NonNull String query) {
49+
List<Suggestion> list = new ArrayList<>();
50+
try {
51+
String autocompleteUrl = AppUrls.getAutocompleteUrl(query);
52+
URL url = new URL(autocompleteUrl);
53+
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
54+
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
55+
StringBuilder stringBuilder = new StringBuilder();
56+
String line;
57+
while((line = reader.readLine()) != null) {
58+
stringBuilder.append(line);
59+
}
60+
reader.close();
61+
JSONArray jsonArray = new JSONArray(stringBuilder.toString());
62+
for(int i=0; i<jsonArray.length(); i++) {
63+
JSONObject jsonObject = jsonArray.getJSONObject(i);
64+
SuggestionJsonEntity suggestion = new SuggestionJsonEntity();
65+
suggestion.fromJson(jsonObject.toString());
66+
//list.add(new SuggestionJsonEntity(jsonObject.getString("phrase")));
67+
list.add(suggestion);
68+
}
69+
} catch(MalformedURLException e) {
70+
Timber.e(e, "getSuggestions, query: %s", query);
71+
} catch(IOException e) {
72+
Timber.e(e, "getSuggestions, query: %s", query);
73+
} catch(JSONException e) {
74+
Timber.e(e, "getSuggestions, query: %s", query);
75+
}
76+
return list;
77+
}
78+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) 2017 DuckDuckGo
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.duckduckgo.app.data.suggestion;
18+
19+
import com.duckduckgo.app.data.base.JsonEntity;
20+
import com.duckduckgo.app.domain.suggestion.Suggestion;
21+
22+
import org.json.JSONException;
23+
import org.json.JSONObject;
24+
25+
import timber.log.Timber;
26+
27+
/**
28+
* Created by fgei on 6/30/17.
29+
*/
30+
31+
public class SuggestionJsonEntity implements Suggestion, JsonEntity {
32+
private String type;
33+
private String suggestion;
34+
35+
public SuggestionJsonEntity() {
36+
}
37+
38+
public SuggestionJsonEntity(Suggestion suggestion) {
39+
this.type = suggestion.getType();
40+
this.suggestion = suggestion.getSuggestion();
41+
}
42+
43+
@Override
44+
public String getType() {
45+
return type;
46+
}
47+
48+
@Override
49+
public String getSuggestion() {
50+
return suggestion;
51+
}
52+
53+
@Override
54+
public String toJson() {
55+
String json = "";
56+
try {
57+
JSONObject jsonObject = new JSONObject();
58+
jsonObject.put(type, suggestion);
59+
json = jsonObject.toString();
60+
} catch(JSONException e) {
61+
Timber.e(e, "toJson");
62+
}
63+
return json;
64+
}
65+
66+
@Override
67+
public void fromJson(String json) {
68+
try {
69+
JSONObject jsonObject = new JSONObject(json);
70+
type = jsonObject.keys().next();
71+
suggestion = jsonObject.getString(type);
72+
} catch (JSONException e) {
73+
Timber.e(e, "fromJson, json: %s", json);
74+
}
75+
}
76+
77+
@Override
78+
public String getKey() {
79+
return null;
80+
}
81+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2017 DuckDuckGo
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.duckduckgo.app.domain.suggestion;
18+
19+
/**
20+
* Created by fgei on 6/30/17.
21+
*/
22+
23+
public interface Suggestion {
24+
String getType();
25+
String getSuggestion();
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2017 DuckDuckGo
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.duckduckgo.app.domain.suggestion;
18+
19+
import android.support.annotation.NonNull;
20+
21+
import java.util.List;
22+
23+
/**
24+
* Created by fgei on 6/30/17.
25+
*/
26+
27+
public interface SuggestionRepository {
28+
List<Suggestion> getSuggestions(@NonNull String query);
29+
}

app/src/main/java/com/duckduckgo/app/util/AppUrls.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class AppUrls {
3434
private class Urls {
3535
static final String BASE = "duckduckgo.com";
3636
static final String HOME = "https://www.duckduckgo.com/?ko=-1&kl=wt-wt";
37+
static final String AUTOCOMPLETE = "https://www.duckduckgo.com/ac/";
3738
}
3839

3940
private class Params {
@@ -96,14 +97,26 @@ public static String getQuery(@NonNull String url) {
9697

9798
@NonNull
9899
public static String getSearchUrl(@NonNull String query) {
100+
StringBuilder builder = new StringBuilder(Urls.HOME);
101+
builder.append("&").append(Params.SEARCH).append("=").append(getEncodedQuery(query));
102+
return builder.toString();
103+
}
104+
105+
@NonNull
106+
public static String getAutocompleteUrl(@NonNull String query) {
107+
StringBuilder builder = new StringBuilder(Urls.AUTOCOMPLETE);
108+
builder.append("/?").append(Params.SEARCH).append("=").append(getEncodedQuery(query));
109+
return builder.toString();
110+
}
111+
112+
@NonNull
113+
private static String getEncodedQuery(@NonNull String query) {
99114
String queryEncoded = query;
100115
try {
101116
queryEncoded = URLEncoder.encode(query, "UTF-8");
102117
} catch (UnsupportedEncodingException e) {
103-
Timber.e(e, "getSearchUrl, query: %s", query);
118+
Timber.e(e, "getEncodedQuery, query: %s", query);
104119
}
105-
StringBuilder builder = new StringBuilder(Urls.HOME);
106-
builder.append("&").append(Params.SEARCH).append("=").append(queryEncoded);
107-
return builder.toString();
120+
return queryEncoded;
108121
}
109122
}

0 commit comments

Comments
 (0)
0