| 
					
				 | 
			
			
				@@ -0,0 +1,838 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+package com.slam.bboxble; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import android.net.Uri; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import android.os.Bundle; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import android.support.annotation.Nullable; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import android.support.v4.app.Fragment; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import android.util.Log; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import android.view.LayoutInflater; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import android.view.View; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import android.view.ViewGroup; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import android.widget.AdapterView; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import android.widget.Button; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import android.widget.CheckBox; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import android.widget.ListView; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import android.widget.ProgressBar; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import android.widget.TextView; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import android.widget.Toast; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.slam.bboxblelib.Conversion; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.io.File; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.io.FileInputStream; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.io.IOException; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.io.InputStream; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.ArrayList; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.HashMap; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.Timer; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.TimerTask; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.concurrent.locks.Lock; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.concurrent.locks.ReentrantLock; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+/** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * Created by lenovo on 2017/9/15. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+public class BekenOTAFragment extends Fragment implements View.OnClickListener { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private final String TAG = BekenOTAFragment.class.getSimpleName(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private Button mButtonToUUID, mButtonOTA, mButtonStartOTA, mButtonStopOTA, mButtonRefresh, mButtonFileList; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private ListView mOTAListView; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private int OTAType = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private final int MaxRetry = 2000; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private HashMap<String, FileInfo> mOTAFlieList = null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private ArrayList<String> mFileList; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//    private int mTimeoutValue = MaxRetry; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private TextView mProgressInfo; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private ProgressBar mProgressBar; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private int mReadyToUpdate = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private long mAlreadyReadCount = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private TextView mTextViewDeviceVersion, mTextViewFileVersion, mTextViewFilePath; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private TextView mTextViewDeviceRomVersion, mTextViewOTARomVersion; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private boolean isOTADone = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private boolean canGo = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private boolean flagTag = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private boolean ifBlockSend = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private static final int FILE_BUFFER_SIZE = 0x40000; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private static final int OAD_BLOCK_SIZE = 16; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private static final int HAL_FLASH_WORD_SIZE = 4; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private static final int OAD_BUFFER_SIZE = 2 + OAD_BLOCK_SIZE; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private final byte[] mFileBuffer = new byte[FILE_BUFFER_SIZE]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private byte[] mOadBuffer = new byte[OAD_BUFFER_SIZE]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private ImgHdr mFileImgHdr = new ImgHdr(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    //private ImgHdr mfileImgRomHdr = new ImgHdr(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private Timer mTimer = null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private ProgInfo mProgInfo = new ProgInfo(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private TimerTask mTimerTask = null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private long lastBlockReq = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private static final long TIMER_INTERVAL = 1000; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private static final int SEND_INTERVAL = 10; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private final Lock mLock = new ReentrantLock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private boolean mProgramming = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private int DelayTimer = 2; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private Uri mFilePath; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private int MAXNotify = 10; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private byte[] mFileIndexBuffer; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private OTAFileAdapter mOTAFileAdapter; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        View view = inflater.inflate(R.layout.fragment_beken_ota, container, false); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mOTAListView = (ListView) view.findViewById(R.id.list_ota_files); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mProgressInfo = (TextView) view.findViewById(R.id.tw_info); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mProgressBar = (ProgressBar) view.findViewById(R.id.pb_progress); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mButtonOTA = (Button) view.findViewById(R.id.button_OtaToOTA); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mButtonToUUID = (Button) view.findViewById(R.id.button_OtaToUUid); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mButtonStartOTA = (Button) view.findViewById(R.id.button_start_ota); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mButtonStopOTA = (Button) view.findViewById(R.id.button_stop_ota); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mButtonRefresh = (Button) view.findViewById(R.id.button_refresh); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mButtonFileList = (Button) view.findViewById(R.id.button); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mTextViewDeviceVersion = (TextView) view.findViewById(R.id.text_DeviceVersion); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mTextViewFileVersion = (TextView) view.findViewById(R.id.textview_OTAVersion); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mTextViewFilePath = (TextView) view.findViewById(R.id.textview_file_path); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mTextViewDeviceRomVersion = (TextView) view.findViewById(R.id.textview_DeviceRomVersion); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mTextViewOTARomVersion = (TextView) view.findViewById(R.id.textview_OTARomVersion); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mButtonFileList.setOnClickListener(this); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mButtonOTA.setOnClickListener(this); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mButtonToUUID.setOnClickListener(this); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mButtonStartOTA.setOnClickListener(this); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mButtonStopOTA.setOnClickListener(this); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mButtonRefresh.setOnClickListener(this); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mButtonStopOTA.setVisibility(View.GONE); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mButtonStartOTA.setEnabled(false); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return view; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        super.onActivityCreated(savedInstanceState); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        prepareDate(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mOTAFileAdapter = new OTAFileAdapter(getContext(), mOTAFlieList, R.layout.listitem_script, mFileList); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mOTAListView.setAdapter(mOTAFileAdapter); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mOTAListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if (!mProgramming) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    mOTAFileAdapter.init(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    mOTAFileAdapter.notifyDataSetChanged(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    CheckBox lCheckBox = (CheckBox) view.getTag(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    lCheckBox.toggle(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    mOTAFileAdapter.isSelected.put(i, lCheckBox.isChecked()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    String filePath = FileUnit.getOTAFilePath() + "/" + mOTAFileAdapter.getCheckedFileName(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    if (lCheckBox.isChecked()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        if(getFileVerion(filePath)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            mTextViewFilePath.setText(mOTAFileAdapter.getCheckedFileName()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            if(!mButtonStartOTA.isEnabled()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                mButtonStartOTA.setEnabled(true); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        }else 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            mOTAFileAdapter.init(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            mOTAFileAdapter.notifyDataSetChanged(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    }else 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        mTextViewFilePath.setText(""); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        mTextViewFileVersion.setText(""); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        mTextViewOTARomVersion.setText(""); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        if(mButtonStartOTA.isEnabled()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            mButtonStartOTA.setEnabled(false); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private void prepareDate() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mOTAFlieList = new HashMap<String, FileInfo>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        File lFolderFile = new File(FileUnit.getOTAFilePath()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mFileList = new ArrayList<String>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (lFolderFile.listFiles() != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            int size = lFolderFile.listFiles().length; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            for (int forCount = 0; forCount < size; forCount++) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                File lFile = lFolderFile.listFiles()[forCount]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                FileInfo lFileInfo = new FileInfo(lFile.toString(), lFile.getName()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if (lFileInfo.getFileName().endsWith(".bin")) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    mFileList.add(lFileInfo.getFileName()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    mOTAFlieList.put(lFile.getName(), lFileInfo); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public void onClick(View v) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        switch (v.getId()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            case R.id.button_OtaToOTA: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            case R.id.button_OtaToUUid: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if (!mProgramming) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    ((DeviceControlActivity) getActivity()).changeFragment(2); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            case R.id.button_start_ota: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                flagTag = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                isOTADone = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if(mTextViewDeviceVersion.getText().length()== 0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    Toast.makeText(getContext(), "check device version fail", Toast.LENGTH_SHORT).show(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    ((DeviceControlActivity) getActivity()).letGetVersionTrigger(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    ((DeviceControlActivity) getActivity()).getmBluetoothLeService().writeOTAIdentfy(new byte[]{0x00}); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    if(MAXNotify > 0) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        MAXNotify--; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    if(MAXNotify == 0) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        ((DeviceControlActivity) getActivity()).ByeByeFunction(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                }else if (mOTAFileAdapter.getCheckedFileName() != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    String filePath = FileUnit.getOTAFilePath() + "/" + mOTAFileAdapter.getCheckedFileName(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    Log.e(TAG, filePath); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    if(OTAType == 2 && mTextViewOTARomVersion.getText().toString().equals(mTextViewDeviceRomVersion.getText().toString())) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        Toast.makeText(getContext(), "rom version the same", Toast.LENGTH_SHORT).show(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    if (OTAType == 1) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        if(mTextViewDeviceVersion.getText().toString().equals(mTextViewFileVersion.getText().toString())) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            Toast.makeText(getContext(), "version the same", Toast.LENGTH_SHORT).show(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        }else if(!mTextViewOTARomVersion.getText().toString().equals(mTextViewDeviceRomVersion.getText().toString())) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            Toast.makeText(getContext(), "rom version not the same", Toast.LENGTH_SHORT).show(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    if(OTAType == 0) break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    loadFile(filePath); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    mProgressBar.setProgress(0); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                    mButtonStartOTA.setVisibility(View.GONE); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                    mButtonStopOTA.setVisibility((View.VISIBLE)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    mButtonStartOTA.setEnabled(false); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    startProgramming(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                }else if (mTextViewFilePath.getText().length() > 0 ){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                    if(mTextViewFilePath.getText().toString().endsWith(".bin")) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    if(OTAType == 2 && mTextViewOTARomVersion.getText().toString().equals(mTextViewDeviceRomVersion.getText().toString())) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        Toast.makeText(getContext(), "rom version the same", Toast.LENGTH_SHORT).show(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    if (OTAType == 1) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        if(mTextViewDeviceVersion.getText().toString().equals(mTextViewFileVersion.getText().toString())) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            Toast.makeText(getContext(), "version the same", Toast.LENGTH_SHORT).show(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        }else if(!mTextViewOTARomVersion.getText().toString().equals(mTextViewDeviceRomVersion.getText().toString())) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            Toast.makeText(getContext(), "rom version not the same", Toast.LENGTH_SHORT).show(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    if(OTAType == 0) break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        loadFile(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        mProgressBar.setProgress(0); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        mButtonStartOTA.setEnabled(false); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        startProgramming(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                    }else 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                        Toast.makeText(getContext(), "not a .bin file", Toast.LENGTH_SHORT).show(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    Toast.makeText(getContext(), "Choose a file first", Toast.LENGTH_SHORT).show(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            case R.id.button_stop_ota: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                mButtonStartOTA.setVisibility(View.VISIBLE); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                mButtonStopOTA.setVisibility((View.GONE)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                //mButtonStartOTA.setEnabled(true); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                stopProgramming(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            case R.id.button_refresh: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if (!mProgramming) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    prepareDate(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    mTextViewFileVersion.setText(""); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    mTextViewDeviceVersion.setText(""); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    mTextViewDeviceRomVersion.setText(""); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    mTextViewFilePath.setText(""); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    mFilePath = null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    ((DeviceControlActivity) getActivity()).letGetVersionTrigger(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    ((DeviceControlActivity) getActivity()).getmBluetoothLeService().writeOTAIdentfy(new byte[]{0x00}); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    mOTAFileAdapter = new OTAFileAdapter(getContext(), mOTAFlieList, R.layout.listitem_script, mFileList); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    mOTAListView.setAdapter(mOTAFileAdapter); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            case R.id.button: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if(!mProgramming) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                    ((DeviceControlActivity) getActivity()).VersTest(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            default: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private class OadTask implements Runnable { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        int x = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        public void run() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            while (mProgramming) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    x = x + SEND_INTERVAL; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    Thread.sleep(SEND_INTERVAL); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } catch (InterruptedException e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    e.printStackTrace(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                //shenzheng beken nubia cell phone can not handle this loading 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                for (int i = 0; (i < 4) & mProgramming; ) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        if (flagTag) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            if (mReadyToUpdate > 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                                flag = programBlock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                mLock.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                programBlock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                mLock.unlock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                i++; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                if(DelayTimer > 2) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                    DelayTimer--; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                x = x + DelayTimer; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                Thread.sleep(DelayTimer); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            } catch (InterruptedException e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                e.printStackTrace(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                //one second update progress once 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if (x >= 1000) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    x = x % 1000; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        ((DeviceControlActivity) getActivity()).runOnUiThread(new Runnable() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            public void run() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                displayStats(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } catch (NullPointerException e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        Log.e(TAG, "something wrong 2"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private void startProgramming() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mProgramming = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        DelayTimer = 2; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        byte[] buf = new byte[16]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        System.arraycopy(mFileBuffer, 0, buf, 0, 16); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        [chengyi| add for match sensorTag] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        byte[] buf = new byte[OAD_IMG_HDR_SIZE + 2 + 2 + 2]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        buf[0] = Conversion.loUint16(mFileImgHdr.ver); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        buf[1] = Conversion.hiUint16(mFileImgHdr.ver); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        buf[2] = 0x00; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        buf[3] = 0x00; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        buf[4] = Conversion.loUint16(mFileImgHdr.len); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        buf[5] = Conversion.hiUint16(mFileImgHdr.len); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        System.arraycopy(mFileImgHdr.uid, 0, buf, 6, 4); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //[chengYi| add for show btye array data] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        StringBuilder stringBuilder = new StringBuilder(buf.length); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        for (byte byteChar : buf) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            stringBuilder.append(String.format("%02x ", byteChar)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Log.e(TAG, "first data for ffc1 " + stringBuilder.toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //[chengYi| end] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // Send image notification 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        boolean success = ((DeviceControlActivity) getActivity()).getmBluetoothLeService().writeOTAIdentfy(buf); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // Initialize stats 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mProgInfo.reset(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // Start the programming thread 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        mReadyToUpdate = BLOCKS_PER_CONNECTION; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mReadyToUpdate = 1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        mReadyToUpdate = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        canGo = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Thread aaa = new Thread(new OadTask()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        aaa.start(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mTimer = new Timer(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mTimerTask = new ProgTimerTask(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mTimer.scheduleAtFixedRate(mTimerTask, 0, TIMER_INTERVAL); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private void programBlock() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (!mProgramming) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ifBlockSend = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (mProgInfo.iBlocks < mProgInfo.nBlocks) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//    if (mProgInfo.iBlocks < 100) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//            mProgramming = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            //String msg = new String(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // Prepare block 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            mOadBuffer = new byte[OAD_BUFFER_SIZE]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            mOadBuffer[0] = Conversion.loUint16(mProgInfo.iBlocks); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            mOadBuffer[1] = Conversion.hiUint16(mProgInfo.iBlocks); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            System.arraycopy(mFileBuffer, (int) mProgInfo.iBytes, mOadBuffer, 2, OAD_BLOCK_SIZE); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if (((DeviceControlActivity) getActivity()).getmBluetoothLeService() != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    if(canGo) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        boolean success = ((DeviceControlActivity) getActivity()).getmBluetoothLeService().writeOTABlock(mOadBuffer); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        if (success) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                            Log.e(TAG, String.valueOf(mProgInfo.iBlocks)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                            mAlreadyReadCount = mProgInfo.iBlocks + 1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                            mProgInfo.iBlocks++; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                            mProgInfo.iBytes = mProgInfo.iBytes + OAD_BLOCK_SIZE; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                            mProgressBar.setProgress((short) ((mProgInfo.iBlocks * 100) / mProgInfo.nBlocks)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                        mTimeoutValue = MaxRetry; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                        } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                        mTimeoutValue--; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            ; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            canGo = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                    mTimeoutValue--; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } catch (NullPointerException e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                //Log.e(TAG, "something wrong"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                mTimeoutValue--; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                mProgramming = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                Log.e(TAG, e.toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            isOTADone = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            mProgramming = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ifBlockSend = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (!mProgramming) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                getActivity().runOnUiThread(new Runnable() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    public void run() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        displayStats(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        mButtonStopOTA.callOnClick(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                    stopProgramming(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            }catch (NullPointerException e) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                Log.e(TAG, e.toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        if(mTimeoutValue <= 0) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//            Log.e(TAG, "max retry"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//            mProgramming = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//            EventBus.getDefault().post(new CheckboxEvent(1)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private void stopProgramming() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (mTimer != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            mTimer.cancel(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            mTimer.purge(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (mTimerTask != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            mTimerTask.cancel(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mTimerTask = null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mProgramming = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private void displayStats() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String txt; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        int byteRate; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        int sec = mProgInfo.iTimeElapsed / 1000; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (sec > 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            byteRate = (int) (mProgInfo.iBytes / sec); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        float timeEstimate; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        timeEstimate = ((float) (mFileImgHdr.len * 4) / (float) mProgInfo.iBytes) * sec; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        txt = String.format("Time: %d / %d sec", sec, (int) timeEstimate); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        txt += String.format("    Bytes: %d (%d/sec)", mProgInfo.iBytes, byteRate); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mProgressInfo.setText(txt); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private class ImgHdr { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        long ver = -1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        long len = -1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        long rom_ver = -1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //Character imgType; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        byte[] uid = new byte[4]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private class ProgTimerTask extends TimerTask { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        public void run() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            mProgInfo.iTimeElapsed += TIMER_INTERVAL; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private class ProgInfo { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        long iBytes = 0; // Number of bytes programmed 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        long iBlocks = 0; // Number of blocks programmed 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        long nBlocks = 0; // Total number of blocks 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        int iTimeElapsed = 0; // Time elapsed in milliseconds 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        void reset() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            iBytes = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            iBlocks = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            iTimeElapsed = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            nBlocks = (short) (mFileImgHdr.len / (OAD_BLOCK_SIZE / HAL_FLASH_WORD_SIZE)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            //System.out.println("nBlocks:"+nBlocks); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public void GETOTANotifyData(byte[] value) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        long blockReq = Conversion.buildUint16(value[1], value[0]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //[chengYi | add log info get back data] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Log.e(TAG, "get back data blockReq: " + blockReq + ", nBlocks:" + mProgInfo.nBlocks); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //[chengYi | end] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (((mProgInfo.nBlocks) == blockReq) && mProgramming) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Log.e(TAG, "nBlock " + mProgInfo.nBlocks + " iBlock " + mProgInfo.iBlocks + "ready to stop OTA"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            mButtonStopOTA.callOnClick(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            //stopProgramming(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (blockReq == 0 && !flagTag) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                flagTag = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                mLock.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                lastBlockReq = blockReq; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                mAlreadyReadCount = blockReq; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                mProgInfo.iBlocks = blockReq; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                mProgInfo.iBytes = (blockReq) * OAD_BLOCK_SIZE; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                mLock.unlock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                mLock.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                lastBlockReq = blockReq; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                mAlreadyReadCount = blockReq; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                mProgInfo.iBlocks = blockReq; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                mProgInfo.iBytes = (blockReq) * OAD_BLOCK_SIZE; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if(DelayTimer < 50) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    DelayTimer = 50; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                mLock.unlock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public void GETVersionData(byte[] value) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        StringBuilder stringBuilder = new StringBuilder(value.length); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        stringBuilder.append(String.format("%02X", value[1])); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        stringBuilder.append(String.format("%02X", value[0])); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Log.e(TAG, stringBuilder.toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mTextViewDeviceVersion.setText(stringBuilder.toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        stringBuilder = new StringBuilder(value.length); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Log.e(TAG, String.valueOf(value.length)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if(value.length == 10) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            stringBuilder.append(String.format("%02X", value[9])); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            stringBuilder.append(String.format("%02X", value[8])); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        }else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            stringBuilder.append("FF"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            stringBuilder.append("FF"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Log.e(TAG, stringBuilder.toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mTextViewDeviceRomVersion.setText(stringBuilder.toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        MAXNotify = 10; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private boolean getFileVerion(String filepath) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mFileIndexBuffer = new byte[16]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // Load binary file 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // Read the file raw into a buffer 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            InputStream stream; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            File f = new File(filepath); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            stream = new FileInputStream(f); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if(stream.available() > 16) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                stream.read(mFileIndexBuffer, 0, 16); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                stream.close(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            }else 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                stream.close(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                return false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } catch (IOException e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // Handle exceptions here 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Log.e(TAG, e.toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if(checkBinCorrect()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            StringBuilder stringBuilder = new StringBuilder(5); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            stringBuilder.append(String.format("%02X", mFileIndexBuffer[5])); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            stringBuilder.append(String.format("%02X", mFileIndexBuffer[4])); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Log.e(TAG, stringBuilder.toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            mTextViewFileVersion.setText(stringBuilder.toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            stringBuilder = new StringBuilder(5); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            stringBuilder.append(String.format("%02X", mFileIndexBuffer[15])); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            stringBuilder.append(String.format("%02X", mFileIndexBuffer[14])); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Log.e(TAG, stringBuilder.toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            mTextViewOTARomVersion.setText(stringBuilder.toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        }else 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Toast.makeText(getContext(), "not a correct bin", Toast.LENGTH_SHORT).show(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private boolean getFileVerion() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mFileIndexBuffer = new byte[16]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // Load binary file 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // Read the file raw into a buffer 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            InputStream stream; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            stream = ((DeviceControlActivity)getActivity()).getContentResolver().openInputStream(mFilePath); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if(stream.available() > 16) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                stream.read(mFileIndexBuffer, 0, 16); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                stream.close(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            }else 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                stream.close(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                return false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } catch (IOException e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // Handle exceptions here 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Log.e(TAG, e.toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if(checkBinCorrect()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            StringBuilder stringBuilder = new StringBuilder(5); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            stringBuilder.append(String.format("%02X", mFileIndexBuffer[5])); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            stringBuilder.append(String.format("%02X", mFileIndexBuffer[4])); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Log.e(TAG, stringBuilder.toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            mTextViewFileVersion.setText(stringBuilder.toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            stringBuilder = new StringBuilder(5); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            stringBuilder.append(String.format("%02X", mFileIndexBuffer[15])); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            stringBuilder.append(String.format("%02X", mFileIndexBuffer[14])); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Log.e(TAG, stringBuilder.toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            mTextViewOTARomVersion.setText(stringBuilder.toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        }else 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Toast.makeText(getContext(), "not a correct bin", Toast.LENGTH_SHORT).show(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private boolean loadFile() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        boolean fSuccess = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // Load binary file 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // Read the file raw into a buffer 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            InputStream stream; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            stream = ((DeviceControlActivity)getActivity()).getContentResolver().openInputStream(mFilePath); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            stream.read(mFileBuffer, 0, mFileBuffer.length); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            stream.close(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } catch (IOException e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // Handle exceptions here 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mFileImgHdr.ver = Conversion.buildUint16(mFileBuffer[5], mFileBuffer[4]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mFileImgHdr.len = Conversion.buildUint16(mFileBuffer[7], mFileBuffer[6]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mFileImgHdr.rom_ver = Conversion.buildUint16(mFileBuffer[15], mFileBuffer[14]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //long templen = mFileImgHdr.len; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //templen |= 0x80000000L; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //mFileImgHdr.imgType = ((mFileImgHdr.ver & 1) == 1) ? 'B' : 'A'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        System.arraycopy(mFileBuffer, 8, mFileImgHdr.uid, 0, 4); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        displayImageInfo(mFileImage, mFileImgHdr); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // Expected duration 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        displayStats(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //mTextViewFileVersion.setText(" " + String.valueOf(mFileImgHdr.ver)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return fSuccess; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private boolean loadFile(String filepath) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        boolean fSuccess = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // Load binary file 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // Read the file raw into a buffer 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            InputStream stream; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            File f = new File(filepath); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            stream = new FileInputStream(f); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            stream.read(mFileBuffer, 0, mFileBuffer.length); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            stream.close(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } catch (IOException e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // Handle exceptions here 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mFileImgHdr.ver = Conversion.buildUint16(mFileBuffer[5], mFileBuffer[4]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mFileImgHdr.len = Conversion.buildUint16(mFileBuffer[7], mFileBuffer[6]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mFileImgHdr.rom_ver = Conversion.buildUint16(mFileBuffer[15], mFileBuffer[14]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //long templen = mFileImgHdr.len; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //templen |= 0x80000000L; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //mFileImgHdr.imgType = ((mFileImgHdr.ver & 1) == 1) ? 'B' : 'A'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        System.arraycopy(mFileBuffer, 8, mFileImgHdr.uid, 0, 4); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        displayImageInfo(mFileImage, mFileImgHdr); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // Expected duration 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        displayStats(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //mTextViewFileVersion.setText(" " + String.valueOf(mFileImgHdr.ver)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return fSuccess; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public void clickStopBtn() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (mProgramming) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            mButtonStopOTA.callOnClick(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public void clickRefreshBtn() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mButtonRefresh.callOnClick(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public boolean ismProgramming() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return mProgramming; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public boolean isOTADone() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return isOTADone; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public void setOutSideFileData(String filename, Uri selectFilePath) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mFilePath = selectFilePath; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mOTAFileAdapter.init(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        mOTAFileAdapter.notifyDataSetChanged(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if(getFileVerion() /*&& filename.endsWith(".bin")*/) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            mTextViewFilePath.setText(filename); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if(!mButtonStartOTA.isEnabled()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                mButtonStartOTA.setEnabled(true); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        }else 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            mFilePath = null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Toast.makeText(getContext(), "not a correct bin", Toast.LENGTH_SHORT).show(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            mTextViewFileVersion.setText(""); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            mTextViewOTARomVersion.setText(""); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            mTextViewFilePath.setText(""); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if(mButtonStartOTA.isEnabled()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                mButtonStartOTA.setEnabled(false); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public boolean checkBinCorrect() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        byte[] buf = new byte[4]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        System.arraycopy(mFileIndexBuffer, 8, buf, 0, 4); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        final StringBuilder stringBuilder = new StringBuilder(5); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        stringBuilder.append(String.format("%02X ", buf[0])); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        stringBuilder.append(String.format("%02X ", buf[1])); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        stringBuilder.append(String.format("%02X ", buf[2])); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        stringBuilder.append(String.format("%02X", buf[3])); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Log.e(TAG, stringBuilder.toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if((buf[0] == 0x42) && (buf[1] == 0x42) && (buf[2] == 0x42) && (buf[3] == 0x42)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            OTAType = 1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            mButtonStartOTA.setText("Partial OTA"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        }else  if((buf[0] == 0x53) && (buf[1] == 0x53) && (buf[2] == 0x53) && (buf[3] == 0x53)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            OTAType = 2; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            mButtonStartOTA.setText("Full OTA"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        }else 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            OTAType = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            mButtonStartOTA.setText("Start OTA"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public void setBlockIndex(int x) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if(mProgramming) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if (x == 1) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    mLock.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    canGo = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    mAlreadyReadCount = mProgInfo.iBlocks + 1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    mProgInfo.iBlocks++; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    mProgInfo.iBytes = mProgInfo.iBytes + OAD_BLOCK_SIZE; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    mLock.unlock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    mProgressBar.setProgress((short) ((mProgInfo.iBlocks * 100) / mProgInfo.nBlocks)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    Log.e(TAG, String.valueOf(mProgInfo.iBlocks)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    mLock.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    canGo = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    mLock.unlock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } catch (NullPointerException e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                Log.e(TAG, e.toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 |