Android Open Source - misound Firmware Manager






From Project

Back to project page misound.

License

The source code is released under:

Apache License

If you think the Android project misound listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.xiaomi.mitv.soundbarapp.upgrade;
//w  w w  .  j  a v a2  s  . c  o  m
import android.content.Context;
import android.util.Log;
import com.xiaomi.mitv.content.FirmwareVersion;
import com.xiaomi.mitv.soundbarapp.provider.DataProvider;
import com.xiaomi.mitv.soundbar.provider.SoundBarORM;

import java.io.*;
import java.net.*;

/**
 * Created by chenxuetong on 6/24/14.
 * the functions in this class are synchronized call
 */
public class FirmwareManager {
    private static final String TAG = "Soundbar_firmware";
    private static final int BUFFER_SIZE = 1024;
    private Context mContext;

    public interface ProgressListener{
        void onProgress(int total, int finished);
    }

    public FirmwareManager(Context context){
        mContext = context;
    }
    //check if the firmware need to upgrade, if true return the Version info,
    //otherwise null
    public FirmwareVersion getNewFirmware(){
        DataProvider provider = new DataProvider();
        return provider.queryNewVersion(getLocalVersionCode());
    }

    public int getLocalVersionCode(){
        return SoundBarORM.getIntValue(mContext, SoundBarORM.dfuCurrentVersionCode, -1);
    }

    public String download(FirmwareVersion version, ProgressListener listener){
        String localFile = null;
        if(version != null){
            boolean downloadOk = true;
            try {
//                if(listener!=null) listener.onBeingDownload(version.size);
                localFile = downloadDFU(version.url, version, listener);
                File savedFile = new File(mContext.getFilesDir(), localFile);
                if(!savedFile.exists()){
                    return null;
                }
                localFile = savedFile.getAbsolutePath();
//                if(listener!=null) listener.onEndDownload(true);
            } catch (MalformedURLException e){
                downloadOk = false;
                Log.e(TAG, "Bad duf url: " + version.url);
                e.printStackTrace();
            } catch (IOException e) {
                downloadOk = false;
                Log.e(TAG, "Failed to download duf firmware file!");
                e.printStackTrace();
            } catch (Throwable t){
                downloadOk = false;
                Log.e(TAG, "Failed to download duf firmware file!");
                t.printStackTrace();
            }

            if(!downloadOk){
//                if(listener!=null) listener.onEndDownload(false);
                return null;
            }
//            if(listener!=null) listener.onBeingUpgrade();

        }
        return localFile;
    }

    //download the dfu file according to url, and save it as file to return
    private String downloadDFU(String url, FirmwareVersion info, ProgressListener listener) throws IOException{
        URL duf = new URL(url);

        Proxy proxy = null;
        URLConnection connection = null;

        if(proxy != null){
            connection = duf.openConnection(proxy);
        }else{
            connection = duf.openConnection();
        }

        connection.setConnectTimeout(10000); //10s
        InputStream in = connection.getInputStream();

        int totalRead = 0;
        //save duf to a local file
        String localFile = "firmware_" + info.codeName + "_" + info.code + ".bin";
        mContext.deleteFile(localFile);
        FileOutputStream saveTo = mContext.openFileOutput(localFile, Context.MODE_PRIVATE);
        byte[] buffer = new byte[BUFFER_SIZE];
        int read = 0;
        while((read=in.read(buffer)) != -1){
            totalRead += read;
            saveTo.write(buffer, 0, read);
            if(read>200 && listener!=null) listener.onProgress(info.size, totalRead);
        }
        if(listener!=null) listener.onProgress(info.size, info.size);
        saveTo.flush();
        saveTo.close();

        String firmwareFile = localFile;
        //try to unzip it
        if(url.toLowerCase().contains(".zip")) {
            FirmwareZipFile pack = new FirmwareZipFile(new File(mContext.getFilesDir().getAbsolutePath(), localFile));
            if(pack.unzip(mContext)) {
                firmwareFile = pack.getDfuFile();
            }
        }
        return firmwareFile;
    }
}




Java Source Code List

com.xiaomi.mitv.soundbar.DefaultMisoundDevice.java
com.xiaomi.mitv.soundbarapp.BarInfoUtils.java
com.xiaomi.mitv.soundbarapp.ConnectingActivity.java
com.xiaomi.mitv.soundbarapp.MainActivity2.java
com.xiaomi.mitv.soundbarapp.SoundBarApplication.java
com.xiaomi.mitv.soundbarapp.TestActivity.java
com.xiaomi.mitv.soundbarapp.TypefaceManager.java
com.xiaomi.mitv.soundbarapp.WrapperActivity.java
com.xiaomi.mitv.soundbarapp.diagnosis.DiagnosisFragment.java
com.xiaomi.mitv.soundbarapp.diagnosis.Engine.java
com.xiaomi.mitv.soundbarapp.diagnosis.ViewWrapper.java
com.xiaomi.mitv.soundbarapp.diagnosis.data.Entry.java
com.xiaomi.mitv.soundbarapp.diagnosis.data.Node.java
com.xiaomi.mitv.soundbarapp.diagnosis.data.OrderedList.java
com.xiaomi.mitv.soundbarapp.diagnosis.data.QAElement.java
com.xiaomi.mitv.soundbarapp.eq.EQManager.java
com.xiaomi.mitv.soundbarapp.eq.EQSettingsFragment.java
com.xiaomi.mitv.soundbarapp.eq.EQStyleResource.java
com.xiaomi.mitv.soundbarapp.eq.EQStyle.java
com.xiaomi.mitv.soundbarapp.eq.UserEQControlFragment.java
com.xiaomi.mitv.soundbarapp.faq.FaqDataStore.java
com.xiaomi.mitv.soundbarapp.faq.FaqFragment.java
com.xiaomi.mitv.soundbarapp.fragment.A2dpDlg.java
com.xiaomi.mitv.soundbarapp.fragment.BaseFragment.java
com.xiaomi.mitv.soundbarapp.fragment.FeedbackFragment.java
com.xiaomi.mitv.soundbarapp.fragment.MainEntryFragment.java
com.xiaomi.mitv.soundbarapp.fragment.PanelFragment.java
com.xiaomi.mitv.soundbarapp.fragment.SettingsFragment.java
com.xiaomi.mitv.soundbarapp.player.MediaButtonIntentReceiver.java
com.xiaomi.mitv.soundbarapp.player.MusicUtils.java
com.xiaomi.mitv.soundbarapp.player.PlayListFragment.java
com.xiaomi.mitv.soundbarapp.player.PlayerFragment.java
com.xiaomi.mitv.soundbarapp.player.PlayerService.java
com.xiaomi.mitv.soundbarapp.player.Player.java
com.xiaomi.mitv.soundbarapp.provider.DataProvider.java
com.xiaomi.mitv.soundbarapp.upgrade.FirmwareManager.java
com.xiaomi.mitv.soundbarapp.upgrade.FirmwareZipFile.java
com.xiaomi.mitv.soundbarapp.upgrade.UpdateActivity.java
com.xiaomi.mitv.soundbarapp.upgrade.UpgradeFragment.java
com.xiaomi.mitv.soundbarapp.upgrade.VersionSelectFragment.java
com.xiaomi.mitv.soundbarapp.util.ConfirmActivityDlg.java
com.xiaomi.mitv.soundbarapp.util.Worker.java
com.xiaomi.mitv.widget.GainView.java
com.xiaomi.mitv.widget.GifView.java
com.xiaomi.mitv.widget.LetterIndexSilderBar.java
com.xiaomi.mitv.widget.MiSwitch.java
com.xiaomi.mitv.widget.RoundAnimationView.java
com.xiaomi.mitv.widget.RoundProgressBar.java
com.xiaomi.mitv.widget.RoundSeekBar.java