-
Notifications
You must be signed in to change notification settings - Fork 62
/
Repository.java
122 lines (99 loc) · 3.69 KB
/
Repository.java
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
/***
Copyright (c) 2017 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
Covered in detail in the book _Android's Architecture Components_
https://commonsware.com/AndroidArch
*/
package com.commonsware.android.diceware;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
class Repository {
private static final String ASSET_FILENAME="eff_short_wordlist_2_0.txt";
private static volatile Repository INSTANCE;
private final Context ctxt;
private final ConcurrentHashMap<Uri, List<String>> cache=new ConcurrentHashMap<>();
private SecureRandom random=new SecureRandom();
private final PwnedCheck checker=new PwnedCheck(new OkHttpClient());
synchronized static Repository get(Context ctxt) {
if (INSTANCE==null) {
INSTANCE=new Repository(ctxt);
}
return(INSTANCE);
}
private Repository(Context ctxt) {
this.ctxt=ctxt.getApplicationContext();
}
Observable<String> getPassphrase(Uri source, final int count) {
return(getWordsFromSource(source)
.map(strings -> (randomSubset(strings, count)))
.map(pieces -> TextUtils.join(" ", pieces))
.flatMap(checker::validate)
.retryWhen(errors -> errors.zipWith(Observable.range(1, 3), (n, i) -> i)));
}
synchronized private Observable<List<String>> getWordsFromSource(Uri source) {
List<String> words=cache.get(source);
final Observable<List<String>> result;
if (words==null) {
result=Observable.just(source)
.subscribeOn(Schedulers.io())
.map(uri -> (open(uri)))
.map(in -> (readWords(in)))
.doOnNext(strings -> cache.put(source, strings));
}
else {
result=Observable.just(words);
}
return(result);
}
private InputStream open(Uri uri) throws IOException {
String scheme=uri.getScheme();
String path=uri.getPath();
if ("file".equals(scheme) && path.startsWith("/android_asset")) {
return(ctxt.getAssets().open(ASSET_FILENAME));
}
ContentResolver cr=ctxt.getContentResolver();
cr.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
return(cr.openInputStream(uri));
}
private static List<String> readWords(InputStream in) throws IOException {
InputStreamReader isr=new InputStreamReader(in);
BufferedReader reader=new BufferedReader(isr);
String line;
List<String> result=new ArrayList<>();
while ((line = reader.readLine())!=null) {
String[] pieces=line.split("\\s");
if (pieces.length==2) {
result.add(pieces[1]);
}
}
return(result);
}
private List<String> randomSubset(List<String> words, int count) {
List<String> result=new ArrayList<>();
int size=words.size();
for (int i=0;i<count;i++) {
result.add(words.get(random.nextInt(size)));
}
return(result);
}
}