Skip to content

Commit 645f218

Browse files
authored
fix(Android): Prevent MissingWebViewPackageException (#119)
1 parent 4c40cd2 commit 645f218

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

android/src/main/java/com/reactnativecommunity/cookies/CookieManagerModule.java

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,21 @@ public class CookieManagerModule extends ReactContextBaseJavaModule {
4343
private static final String CLEAR_BY_NAME_NOT_SUPPORTED = "Cannot remove a single cookie by name on Android";
4444
private static final String INVALID_DOMAINS = "Cookie URL host %s and domain %s mismatched. The cookie won't set correctly.";
4545

46-
private CookieManager mCookieManager;
4746
private CookieSyncManager mCookieSyncManager;
4847

4948
CookieManagerModule(ReactApplicationContext context) {
5049
super(context);
5150
this.mCookieSyncManager = CookieSyncManager.createInstance(context);
52-
this.mCookieManager = CookieManager.getInstance();
53-
mCookieManager.setAcceptCookie(true);
51+
}
52+
53+
private CookieManager getCookieManager() throws Exception {
54+
try {
55+
CookieManager cookieManager = CookieManager.getInstance();
56+
cookieManager.setAcceptCookie(true);
57+
return cookieManager;
58+
} catch (Exception e) {
59+
throw new Exception(e);
60+
}
5461
}
5562

5663
public String getName() {
@@ -88,7 +95,7 @@ public void setFromResponse(String url, String cookie, final Promise promise) {
8895
@ReactMethod
8996
public void flush(Promise promise) {
9097
try {
91-
mCookieManager.flush();
98+
getCookieManager().flush();
9299
promise.resolve(true);
93100
} catch (Exception e) {
94101
promise.reject(e);
@@ -112,7 +119,7 @@ public void get(String url, Boolean useWebKit, Promise promise) {
112119
return;
113120
}
114121
try {
115-
String cookiesString = mCookieManager.getCookie(url);
122+
String cookiesString = getCookieManager().getCookie(url);
116123

117124
WritableMap cookieMap = createCookieList(cookiesString);
118125
promise.resolve(cookieMap);
@@ -128,36 +135,42 @@ public void clearByName(String url, String name, Boolean useWebKit, final Promis
128135

129136
@ReactMethod
130137
public void clearAll(Boolean useWebKit, final Promise promise) {
131-
if (USES_LEGACY_STORE) {
132-
mCookieManager.removeAllCookie();
133-
mCookieManager.removeSessionCookie();
134-
mCookieSyncManager.sync();
135-
promise.resolve(true);
136-
} else {
137-
mCookieManager.removeAllCookies(new ValueCallback<Boolean>() {
138-
@Override
139-
public void onReceiveValue(Boolean value) {
140-
promise.resolve(value);
141-
}
142-
});
143-
mCookieManager.flush();
138+
try {
139+
CookieManager cookieManager = getCookieManager();
140+
if (USES_LEGACY_STORE) {
141+
cookieManager.removeAllCookie();
142+
cookieManager.removeSessionCookie();
143+
mCookieSyncManager.sync();
144+
promise.resolve(true);
145+
} else {
146+
cookieManager.removeAllCookies(new ValueCallback<Boolean>() {
147+
@Override
148+
public void onReceiveValue(Boolean value) {
149+
promise.resolve(value);
150+
}
151+
});
152+
cookieManager.flush();
153+
}
154+
} catch (Exception e) {
155+
promise.reject(e);
144156
}
145157
}
146158

147159
private void addCookies(String url, String cookieString, final Promise promise) {
148160
try {
161+
CookieManager cookieManager = getCookieManager();
149162
if (USES_LEGACY_STORE) {
150-
mCookieManager.setCookie(url, cookieString);
163+
cookieManager.setCookie(url, cookieString);
151164
mCookieSyncManager.sync();
152165
promise.resolve(true);
153166
} else {
154-
mCookieManager.setCookie(url, cookieString, new ValueCallback<Boolean>() {
167+
cookieManager.setCookie(url, cookieString, new ValueCallback<Boolean>() {
155168
@Override
156169
public void onReceiveValue(Boolean value) {
157170
promise.resolve(value);
158171
}
159172
});
160-
mCookieManager.flush();
173+
cookieManager.flush();
161174
}
162175
} catch (Exception e) {
163176
promise.reject(e);

0 commit comments

Comments
 (0)