@Override public void onCreate() { Log.d(TAG, "onCreate"); super.onCreate(); // register broadcast IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_PACKAGE_ADDED); filter.addAction(Intent.ACTION_PACKAGE_REMOVED); filter.addDataScheme("package"); IntentFilter filter2 = new IntentFilter(DreamConstant.SEND_FILE_ACTION); registerReceiver(transferReceiver, filter); registerReceiver(transferReceiver, filter2); mNotice = new Notice(this); mFileInfoManager = new FileInfoManager(this); mHistoryManager = new HistoryManager(this); mSocketMgr = SocketCommunicationManager.getInstance(this); mAppId = AppUtil.getAppID(this); Log.d(TAG, "mappid=" + mAppId); mSocketMgr.registerOnFileTransportListener(this, mAppId); mUserManager = UserManager.getInstance(); pm = getPackageManager(); }
@Override public void onReceiveFile(FileReceiver fileReceiver) { String fileName = fileReceiver.getFileTransferInfo().getFileName(); String sendUserName = fileReceiver.getSendUser().getUserName(); File file = null; long fileSize = fileReceiver.getFileTransferInfo().getFileSize(); Log.d(TAG, "onReceiveFile:" + fileName + "," + sendUserName + ",size=" + fileSize); // define a file to save the receive file File fileDir = new File(DreamConstant.DREAMLINK_FOLDER); if (!fileDir.exists()) { fileDir.mkdirs(); } String filePath = DreamConstant.DREAMLINK_FOLDER + File.separator + fileName; file = new File(filePath); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { Log.e(TAG, "create file error:" + e.toString()); mNotice.showToast("can not create the file:" + fileName); return; } } else { // if file is exist,auto rename fileName = FileInfoManager.autoRename(fileName); while (new File(DreamConstant.DREAMLINK_FOLDER + File.separator + fileName).exists()) { fileName = FileInfoManager.autoRename(fileName); } filePath = DreamConstant.DREAMLINK_FOLDER + File.separator + fileName; file = new File(filePath); } HistoryInfo historyInfo = new HistoryInfo(); historyInfo.setFile(file); historyInfo.setFileSize(fileSize); historyInfo.setSendUserName(sendUserName); historyInfo.setReceiveUser(mUserManager.getLocalUser()); historyInfo.setMsgType(HistoryManager.TYPE_RECEIVE); historyInfo.setDate(System.currentTimeMillis()); historyInfo.setStatus(HistoryManager.STATUS_PRE_RECEIVE); historyInfo = mFileInfoManager.getHistoryInfo(historyInfo); ContentValues values = mHistoryManager.getInsertValues(historyInfo); Uri uri = getContentResolver().insert(MetaData.History.CONTENT_URI, values); Object key = new Object(); mTransferMap.put(key, uri); Log.d(TAG, "onReceiveFile.mTransferMap.size=" + mTransferMap.size()); fileReceiver.receiveFile(file, FileTransferService.this, key); }