mirror of
				https://gitcode.com/gh_mirrors/re/react-native-pushy.git
				synced 2025-10-31 21:33:12 +08:00 
			
		
		
		
	Implement blockupdate on android
This commit is contained in:
		| @@ -7,6 +7,8 @@ import android.content.pm.PackageManager; | |||||||
| import android.util.Log; | import android.util.Log; | ||||||
| import com.facebook.react.ReactInstanceManager; | import com.facebook.react.ReactInstanceManager; | ||||||
|  |  | ||||||
|  | import java.util.HashMap; | ||||||
|  | import java.util.Map; | ||||||
| import java.util.concurrent.Executor; | import java.util.concurrent.Executor; | ||||||
| import java.util.concurrent.Executors; | import java.util.concurrent.Executors; | ||||||
|  |  | ||||||
| @@ -68,6 +70,17 @@ public class UpdateContext { | |||||||
|         return context.getString(R.string.pushy_build_time); |         return context.getString(R.string.pushy_build_time); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     public String getUuid() { | ||||||
|  |         return sp.getString("uuid", null); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public Map getBlockUpdate() { | ||||||
|  |         return new HashMap<String, Object>() {{ | ||||||
|  |             put("until", sp.getInt("blockUntil", 0)); | ||||||
|  |             put("reason", sp.getString("blockReason", null)); | ||||||
|  |         }}; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     public boolean getIsUsingBundleUrl() { |     public boolean getIsUsingBundleUrl() { | ||||||
|         return isUsingBundleUrl; |         return isUsingBundleUrl; | ||||||
|     } |     } | ||||||
| @@ -130,6 +143,19 @@ public class UpdateContext { | |||||||
|         editor.apply(); |         editor.apply(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     public void setUuid(String uuid) { | ||||||
|  |         SharedPreferences.Editor editor = sp.edit(); | ||||||
|  |         editor.putString("uuid", uuid); | ||||||
|  |         editor.apply(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public void setBlockUpdate(int until, String reason) { | ||||||
|  |         SharedPreferences.Editor editor = sp.edit(); | ||||||
|  |         editor.putInt("blockUntil", until); | ||||||
|  |         editor.putString("blockReason", reason); | ||||||
|  |         editor.apply(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     public String getCurrentVersion() { |     public String getCurrentVersion() { | ||||||
|         return sp.getString("currentVersion", null); |         return sp.getString("currentVersion", null); | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -57,6 +57,8 @@ public class UpdateModule extends ReactContextBaseJavaModule{ | |||||||
|         if (isRolledBack) { |         if (isRolledBack) { | ||||||
|             updateContext.clearRollbackMark(); |             updateContext.clearRollbackMark(); | ||||||
|         } |         } | ||||||
|  |         constants.put("blockUpdate", updateContext.getBlockUpdate()); | ||||||
|  |         constants.put("uuid", updateContext.getUuid()); | ||||||
|         return constants; |         return constants; | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -184,6 +186,28 @@ public class UpdateModule extends ReactContextBaseJavaModule{ | |||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     @ReactMethod | ||||||
|  |     public void setBlockUpdate(ReadableMap options) { | ||||||
|  |         final int until = options.getInt("until"); | ||||||
|  |         final String reason = options.getString("reason"); | ||||||
|  |         UiThreadUtil.runOnUiThread(new Runnable() { | ||||||
|  |             @Override | ||||||
|  |             public void run() { | ||||||
|  |                 updateContext.setBlockUpdate(until, reason); | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @ReactMethod | ||||||
|  |     public void setUuid(final String uuid) { | ||||||
|  |         UiThreadUtil.runOnUiThread(new Runnable() { | ||||||
|  |             @Override | ||||||
|  |             public void run() { | ||||||
|  |                 updateContext.setUuid(uuid); | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /* 发送事件*/ |     /* 发送事件*/ | ||||||
|     public static void sendEvent(String eventName,  WritableMap params) { |     public static void sendEvent(String eventName,  WritableMap params) { | ||||||
|         ((ReactContext) mContext).getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, |         ((ReactContext) mContext).getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, | ||||||
|   | |||||||
							
								
								
									
										40
									
								
								lib/index.js
									
									
									
									
									
								
							
							
						
						
									
										40
									
								
								lib/index.js
									
									
									
									
									
								
							| @@ -9,7 +9,7 @@ const { | |||||||
|   version: v, |   version: v, | ||||||
| } = require('react-native/Libraries/Core/ReactNativeVersion'); | } = require('react-native/Libraries/Core/ReactNativeVersion'); | ||||||
| const RNVersion = `${v.major}.${v.minor}.${v.patch}`; | const RNVersion = `${v.major}.${v.minor}.${v.patch}`; | ||||||
| import { v4 as uuidv4 } from 'uuid'; | const uuidv4 = require('uuid/v4'); | ||||||
|  |  | ||||||
| let Pushy = NativeModules.Pushy; | let Pushy = NativeModules.Pushy; | ||||||
|  |  | ||||||
| @@ -70,16 +70,26 @@ function assertRelease() { | |||||||
|  |  | ||||||
| export async function checkUpdate(APPKEY, isRetry) { | export async function checkUpdate(APPKEY, isRetry) { | ||||||
|   assertRelease(); |   assertRelease(); | ||||||
|   if (blockUpdate && blockUpdate.until > Date.now()) { |   if (blockUpdate && blockUpdate.until > Date.now() / 1000) { | ||||||
|     console.warn( |     throw new Error( | ||||||
|       `Pushy update is blocked until ${new Date( |       `热更新已暂停,原因:${blockUpdate.reason}。请在"${new Date( | ||||||
|         blockUpdate.until, |         blockUpdate.until * 1000, | ||||||
|       ).toLocaleString()}. Reason: ${blockUpdate.reason}`, |       ).toLocaleString()}"之后重试。`, | ||||||
|     ); |     ); | ||||||
|     return; |  | ||||||
|   } |   } | ||||||
|   let resp; |   let resp; | ||||||
|   try { |   try { | ||||||
|  |     console.warn({ | ||||||
|  |       packageVersion, | ||||||
|  |       hash: currentVersion, | ||||||
|  |       buildTime, | ||||||
|  |       cInfo: { | ||||||
|  |         pushy: require('../package.json').version, | ||||||
|  |         rn: RNVersion, | ||||||
|  |         os: Platform.OS + ' ' + Platform.Version, | ||||||
|  |         uuid, | ||||||
|  |       }, | ||||||
|  |     }); | ||||||
|     resp = await fetch(getCheckUrl(APPKEY), { |     resp = await fetch(getCheckUrl(APPKEY), { | ||||||
|       method: 'POST', |       method: 'POST', | ||||||
|       headers: { |       headers: { | ||||||
| @@ -105,25 +115,25 @@ export async function checkUpdate(APPKEY, isRetry) { | |||||||
|     await tryBackupEndpoints(APPKEY); |     await tryBackupEndpoints(APPKEY); | ||||||
|     return checkUpdate(APPKEY, true); |     return checkUpdate(APPKEY, true); | ||||||
|   } |   } | ||||||
|  |   const result = await resp.json(); | ||||||
|   checkOperation(resp); |   checkOperation(result.op); | ||||||
|  |  | ||||||
|   if (resp.status !== 200) { |   if (resp.status !== 200) { | ||||||
|     throw new Error((await resp.json()).message); |     throw new Error(result.message); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   return resp.json(); |   return result; | ||||||
| } | } | ||||||
|  |  | ||||||
| function checkOperation(resp) { | function checkOperation(op) { | ||||||
|   if (!Array.isArray(resp.op)) { |   if (!Array.isArray(op)) { | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
|   resp.op.forEach((action) => { |   op.forEach((action) => { | ||||||
|     if (action.type === 'block') { |     if (action.type === 'block') { | ||||||
|       blockUpdate = { |       blockUpdate = { | ||||||
|         reason: action.reason, |         reason: action.reason, | ||||||
|         until: Date.now() + action.duration, |         until: (Date.now() + action.duration) / 1000, | ||||||
|       }; |       }; | ||||||
|       Pushy.setBlockUpdate(blockUpdate); |       Pushy.setBlockUpdate(blockUpdate); | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -27,6 +27,6 @@ | |||||||
|   }, |   }, | ||||||
|   "homepage": "https://github.com/reactnativecn/react-native-pushy#readme", |   "homepage": "https://github.com/reactnativecn/react-native-pushy#readme", | ||||||
|   "dependencies": { |   "dependencies": { | ||||||
|     "uuid": "^8.3.0" |     "uuid": "3" | ||||||
|   } |   } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -2,7 +2,7 @@ | |||||||
| # yarn lockfile v1 | # yarn lockfile v1 | ||||||
|  |  | ||||||
|  |  | ||||||
| uuid@^8.3.0: | uuid@3: | ||||||
|   version "8.3.0" |   version "3.4.0" | ||||||
|   resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.0.tgz#ab738085ca22dc9a8c92725e459b1d507df5d6ea" |   resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee" | ||||||
|   integrity sha512-fX6Z5o4m6XsXBdli9g7DtWgAx+osMsRRZFKma1mIUsLCz6vRvv+pz5VNbyu9UEDzpMWulZfvpgb/cmDXVulYFQ== |   integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A== | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 sunnylqm
					sunnylqm