记录一次代码分析(1)


入口Activity如下(由于有些涉及隐私,所以不展示):


  protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    setContentView(R.layout.activity_splash_screen);
    getDeviceDimensions(this);
    sharedPrefUtil = new SharedPrefUtil(getApplicationContext());
    PlayerUtil.setSelectedPlayer(this, null);
    new Handler().postDelayed(new Runnable()
    {
      public void run()
      {
        if (sharedPrefUtil.getBoolean("LOGIN_USER_STATUS"))
        {
          localIntent = new Intent(SplashScreenActivity.this, ManagePlayerActivity.class);
          localIntent.putExtra("EXTRA_SELECT_PLAYER", true);
          localIntent.putExtra("EXTRA_IS_FIRST_TIME", true);
          startActivityForResult(localIntent, 0);
          return;
        }
        if (!sharedPrefUtil.getBoolean("HAVE_PLAYSET"))
        {
          localIntent = new Intent(SplashScreenActivity.this, BeforeStartActivity.class);
          startActivityForResult(localIntent, 0);
          return;
        }
        if (!sharedPrefUtil.getBoolean("DO_YOU_HAVE_ACCOUNT"))
        {
          localIntent = new Intent(SplashScreenActivity.this, HaveAccountActivity.class);
          startActivityForResult(localIntent, 0);
          return;
        }
        Intent localIntent = new Intent(SplashScreenActivity.this, LoginActivity.class);
        startActivityForResult(localIntent, 0);
      }
    }, SPLASH_TIME_OUT);
  }

其中getDeviceDimensions为适配屏幕分辨率:


  public static void getDeviceDimensions(Activity paramActivity)
  {
    paramActivity = paramActivity.getWindowManager().getDefaultDisplay();
    Point localPoint = new Point();
    paramActivity.getSize(localPoint);
    Constants.setDeviceWidth(x);
    Constants.setDeviceHeight(y);
  }

sharedPrefUtil用来本地存储,主要通过SharedPreferences来实现的,该程序主要包含的本地存储对象为:


BATTER_SAVER
DO_YOU_HAVE_ACCOUNT
FILE_PATH
FIRMWARE_RIVISION
FIRMWARE_VERSION
HARDWARE_RIVISION
HAVE_PLAYSET
IS_ALL_SET
IS_IDLE_TIME
LOGIN_USER_ID
LOGIN_USER_INFO
LOGIN_USER_STATUS
LOGIN_USER_TOKEN
PLAYERS_LIMIT
PLAYSET_ADDRESS
PLAYSET_IDLE_TIME
PLAYSET_MANUFACTURER
PLAYSET_MODEL
PLAYSET_NAME
PRIVACY
REMEMBER_PLAYSET
SELECTED_PLAYER
TERMS_AND_CONDITIONS
USER_ID
USER_NAME
USER_PASSWORD

如果存在LOGIN_USER_STATUS则跳转ManagePlayerActivity,并传递参数EXTRA_SELECT_PLAYER和EXTRA_IS_FIRST_TIME参数。如果没有用户则需要创建用户,有用户需要登录。下一步主要来看下ManagePlayerActivity。

  • ManagePlayerActivity
  • 类定义及继承关系

    
      public class ManagePlayerActivity
        extends BluetoothBaseActivity
        implements View.OnClickListener, ApiCallListener {
         ...
      }
    

    接口ApiCallListener定义如下

    
    public abstract interface ApiCallListener
    {
      public abstract void onFailure(Object paramObject);
      public abstract void onResponse(Object paramObject);
    }
    

    ManagePlayerActivity继承BluetoothBaseActivity

  • BluetoothBaseActivity
  • 
    public abstract class BluetoothBaseActivity
      extends AppCompatActivity
      implements BluetoothCharacteristicListener, LoginListener, DialogInterface.OnDismissListener, PlaysetConnectionListener
    {
      ...
    }
    

    继承AppCompatActivity,可以说是新版的Activity(support v7中更新的),主要继承的借口有
    BluetoothCharacteristicListener

    
    public abstract interface BluetoothCharacteristicListener
    {
      public abstract void bleAvailableServices();
      public abstract void bleBatteryLevel(String paramString);
      public abstract void bleConnectedPlayset();
      public abstract void bleDisconnectedPlayset();
      public abstract void bleFirmwareUpdateStatus(int paramInt);
      public abstract void bleLcdsStates(byte[] paramArrayOfByte);
      public abstract void bleLettersFromPlayset(String paramString, byte[] paramArrayOfByte);
      public abstract void blePlaysetFirmwareRevision(String paramString);
      public abstract void blePlaysetHardwareRevision(String paramString);
      public abstract void blePlaysetManufacturerName(String paramString);
      public abstract void blePlaysetModelNumber(String paramString);
      public abstract void blePlaysetName(String paramString);
    }
    

    LoginListener

    
    public abstract interface LoginListener
    {
      public abstract void didFinishSdkUserConfiguration();
      public abstract void didSelectedChild(Player paramPlayer);
    }
    

    Player就是包含用户多种属性的序列化类

    
      @SerializedName("activityDate")
      private String activityDate;
      @SerializedName("avatar")
      private String avatar;
      @SerializedName("createdAt")
      private String createdAt;
      @SerializedName("currentLevel")
      private String currentLevel;
      @SerializedName("dateofBirth")
      private String dateofBirth;
      @SerializedName("firstName")
      private String firstName;
      @SerializedName("game")
      private String game;
      @SerializedName("gender")
      private String gender;
      @SerializedName("id")
      private String id;
      @SerializedName("imgbase64")
      private String imgbase64;
      @SerializedName("owner")
      private String owner;
      @SerializedName("profileURL")
      private String profileURL;
      @SerializedName("status")
      private String status;
      @SerializedName("updatedAt")
      private String updatedAt;
    

    PlaysetConnectionListener

    
    public abstract interface PlaysetConnectionListener
    {
      public abstract void availableServices();
      public abstract void batteryLevel(String paramString);
      public abstract void connectedPlayset();
      public abstract void didClearTheCharctersOnBoard();
      public abstract void disconnectedPlayset();
      public abstract void firmwareUpdateStatus(int paramInt);
      public abstract void gamePaused(boolean paramBoolean);
      public abstract void lcdsStates(byte[] paramArrayOfByte);
      public abstract void lettersFromPlayset(String paramString, byte[] paramArrayOfByte);
      public abstract void playsetFirmwareRevision(String paramString);
      public abstract void playsetHardwareRevision(String paramString);
      public abstract void playsetManufacturerName(String paramString);
      public abstract void playsetModelNumber(String paramString);
      public abstract void playsetName(String paramString);
    }
    

    下面我们重点看BluetoothBaseActivity的类实现,这个对我们理解所有的工作流程有非常的帮助!

    成员变量

    
      // 用户Connection监听器
      private static PlaysetConnectionListener playsetConnectionListener;
      // 是否锁屏
      private boolean isScreenLocked = true;
      private View mDecorView;
      private ProgressDialog progressDialog;
      // 本地存储
      private SharedPrefUtil sharedPrefUtil;
    

    关键成员函数

      onCreate
    
      protected void onCreate(Bundle paramBundle)
      {
        requestWindowFeature(1);
        super.onCreate(paramBundle);
        getWindow().setFlags(1024, 1024);
        mDecorView = getWindow().getDecorView();
        hideSystemUI();
        Util.IS_APP_RUNNING = true;
        // 蓝牙初始化,有关BluetoothUtil我们之后说
        if (!BluetoothUtil.isInitialized()) {
          BluetoothUtil.initialize(getApplicationContext());
        }
        // 设置蓝牙监听器
        BluetoothUtil.setListener(this);
        // 设置本地存储
        sharedPrefUtil = new SharedPrefUtil(this);
        if (progressDialog == null)
        {
          progressDialog = new ProgressDialog(this, R.style.SPProgressDialogTheme);
          progressDialog.setOnDismissListener(this);
        }
        // 未设置分辨率,设置分辨率,用于屏幕适配
        if (Constants.getDeviceWidth() == 0) {
          SPSplashScreenActivity.getDeviceDimensions(this);
        }
        new Handler().postDelayed(new Runnable()
        {
          public void run()
          {
            // 配置UI
            ViewGroup localViewGroup = (ViewGroup)((ViewGroup)findViewById(16908290)).getChildAt(0);
            BluetoothBaseActivity.this.setupUI(localViewGroup);
          }
        }, 1000L);
      }
    
      onResume

    Android生命周期图如下:

    
      protected void onResume()
      {
        super.onResume();
        // 设置监听器
        BluetoothUtil.setListener(this);
        ...
        // 下面这里非常非常重要!!!
        try
        {
    
          Class localClass = Class.forName(getString(R.string.game_activity));
          if (getClass().equals(localClass))
          {
            LoginActivity.setListener(this);
            ManagePlayerActivity.setListener(this);
            setplaysetConnectionListener(this);
          }
          ...
          return;
        }
        catch (Exception localException)
        {
          ...
        }
      }
    

    我们需要分析上面这段内容

    getString:获得定义的字符串在res/string.xml中。满足当前运行类是指定game_activity时,配置监听器!

      LoginListener中接口的实现
    
    // 空实现
    public void didFinishSdkUserConfiguration() {}
    public void didSelectedChild(Player paramPlayer) {}
    
      PlaysetConnectionListener中接口的实现
    
    // 空实现
    public void availableServices() {}
    public void didClearTheCharctersOnBoard() {}
    
      BluetoothCharacteristicListener中接口的实现
    
      public void bleAvailableServices()
      {
        if (playsetConnectionListener != null) {
          playsetConnectionListener.availableServices();
        }
      }
      
      // 获取电量等级
      public void bleBatteryLevel(String paramString)
      {
        if (playsetConnectionListener != null) {
          playsetConnectionListener.batteryLevel(paramString);
        }
        int i = Util.parseInt(paramString);
        if (i < 10)
        {
          String str1 = getString(R.string.Oops);
          String str2 = getString(R.string.LowBatteryMsg);
          if (i == 0) {
            paramString = "1";
          }
          Util.showAlert(this, str1, str2.replace("X", paramString), getString(R.string.Ok), null);
        }
      }
      //
      public void bleConnectedPlayset()
      {
        if (playsetConnectionListener != null) {
          playsetConnectionListener.connectedPlayset();
        }
      }
      //
      public void bleDisconnectedPlayset()
      {
        if (playsetConnectionListener != null) {
          playsetConnectionListener.disconnectedPlayset();
        }
        if (...)
        {
          ...
          try
          {
            ...
            PlayerUtil.startPlaysetScanActivity(this);
            return;
          }
          catch (ClassNotFoundException localClassNotFoundException)
          {
            localClassNotFoundException.printStackTrace();
          }
        }
      }
    
      // 
      public void bleFirmwareUpdateStatus(int paramInt)
      {
        if (playsetConnectionListener != null) {
          playsetConnectionListener.firmwareUpdateStatus(paramInt);
        }
      }
      
      public void bleLcdsStates(byte[] paramArrayOfByte)
      {
        if (playsetConnectionListener != null) {
          playsetConnectionListener.lcdsStates(paramArrayOfByte);
        }
      }
    
      public void bleLettersFromPlayset(String paramString, byte[] paramArrayOfByte)
      {
        if (playsetConnectionListener != null)
        {
          playsetConnectionListener.lettersFromPlayset(paramString, paramArrayOfByte);
          if (paramString.trim().length() == 0) {
            playsetConnectionListener.didClearTheCharctersOnBoard();
          }
        }
      }
    
      public void blePlaysetFirmwareRevision(String paramString)
      {
        if (playsetConnectionListener != null) {
          playsetConnectionListener.playsetFirmwareRevision(paramString);
        }
        sharedPrefUtil.setString("FIRMWARE_RIVISION", paramString);
      }
    
      public void blePlaysetHardwareRevision(String paramString)
      {
        if (playsetConnectionListener != null) {
          playsetConnectionListener.playsetHardwareRevision(paramString);
        }
        sharedPrefUtil.setString("HARDWARE_RIVISION", paramString);
      }
    
      public void blePlaysetManufacturerName(String paramString)
      {
        if (playsetConnectionListener != null) {
          playsetConnectionListener.playsetManufacturerName(paramString);
        }
        sharedPrefUtil.setString("PLAYSET_MANUFACTURER", paramString);
      }
    
      public void blePlaysetModelNumber(String paramString)
      {
        if (playsetConnectionListener != null) {
          playsetConnectionListener.playsetModelNumber(paramString);
        }
        sharedPrefUtil.setString("PLAYSET_MODEL", paramString);
      }
    
      public void blePlaysetName(String paramString)
      {
        if (playsetConnectionListener != null) {
          playsetConnectionListener.playsetName(paramString);
        }
      }
    

    至此我们分析了BluetoothBaseActivity,下一节我们主要分析ManagePlayerActivity

    欢迎留言

    avatar
      Subscribe  
    Notify of