4141import java .io .InputStream ;
4242import java .io .ByteArrayOutputStream ;
4343import java .io .ByteArrayInputStream ;
44+ import org .apache .cordova .CordovaPreferences ;
4445
4546public class Wechat extends CordovaPlugin {
4647
@@ -98,13 +99,14 @@ public class Wechat extends CordovaPlugin {
9899 protected static CallbackContext currentCallbackContext ;
99100 protected static IWXAPI wxAPI ;
100101 protected static String appId ;
102+ protected static CordovaPreferences wx_preferences ;
101103
102104 @ Override
103105 protected void pluginInitialize () {
104106
105107 super .pluginInitialize ();
106108
107- String id = getAppId ();
109+ String id = getAppId (preferences );
108110
109111 // save app id
110112 saveAppId (cordova .getActivity (), id );
@@ -117,9 +119,11 @@ protected void pluginInitialize() {
117119
118120 protected void initWXAPI () {
119121 IWXAPI api = getWxAPI (cordova .getActivity ());
120-
122+ if (wx_preferences == null ) {
123+ wx_preferences = preferences ;
124+ }
121125 if (api != null ) {
122- api .registerApp (getAppId ());
126+ api .registerApp (getAppId (preferences ));
123127 }
124128 }
125129
@@ -280,7 +284,7 @@ protected boolean sendPaymentRequest(CordovaArgs args, CallbackContext callbackC
280284
281285 try {
282286 final String appid = params .getString ("appid" );
283- final String savedAppid = getAppId (cordova .getActivity ());
287+ final String savedAppid = getSavedAppId (cordova .getActivity ());
284288 if (!savedAppid .equals (appid )) {
285289 this .saveAppId (cordova .getActivity (), appid );
286290 }
@@ -332,7 +336,7 @@ protected boolean chooseInvoiceFromWX(CordovaArgs args, CallbackContext callback
332336 ChooseCardFromWXCardPackage .Req req = new ChooseCardFromWXCardPackage .Req ();
333337
334338 try {
335- req .appId = getAppId ();
339+ req .appId = getAppId (preferences );
336340 req .cardType = "INVOICE" ;
337341 req .signType = params .getString ("signType" );
338342 req .cardSign = params .getString ("cardSign" );
@@ -541,10 +545,7 @@ protected Bitmap getBitmap(JSONObject message, String key, int maxSize) {
541545
542546 /**
543547 * compress bitmap by quility
544- *
545- * @param url
546- * @return
547- */
548+ */
548549 protected Bitmap compressImage (Bitmap image ,Integer maxSize ) {
549550
550551 ByteArrayOutputStream baos = new ByteArrayOutputStream ();
@@ -563,9 +564,6 @@ protected Bitmap compressImage(Bitmap image,Integer maxSize) {
563564
564565 /**
565566 * Get input stream from a url
566- *
567- * @param url
568- * @return
569567 */
570568 protected InputStream getFileInputStream (String url ) {
571569 try {
@@ -626,9 +624,13 @@ protected InputStream getFileInputStream(String url) {
626624 return null ;
627625 }
628626
629- public static String getAppId () {
627+ public static String getAppId (CordovaPreferences f_preferences ) {
630628 if (appId == null ) {
631- appId = preferences .getString (WXAPPID_PROPERTY_KEY , "" );
629+ if (f_preferences != null ) {
630+ appId = f_preferences .getString (WXAPPID_PROPERTY_KEY , "" );
631+ }else if (wx_preferences != null ){
632+ appId = wx_preferences .getString (WXAPPID_PROPERTY_KEY , "" );
633+ }
632634 }
633635
634636 return appId ;
@@ -650,7 +652,7 @@ public static String getSavedAppId(Context ctx) {
650652 * @param id
651653 */
652654 public static void saveAppId (Context ctx , String id ) {
653- if (id .isEmpty ()) {
655+ if (id != null && id .isEmpty ()) {
654656 return ;
655657 }
656658
0 commit comments