@Override
 public void onReceive(Context context, Intent intent) {
   try {
     DownloaderClientMarshaller.startDownloadServiceIfRequired(
         context, intent, MapFileDownloaderService.class);
   } catch (NameNotFoundException e) {
     Log.e("apk-expansion-files", "NameNotFoundException occurred. " + e.getMessage(), e);
   }
 } // onReceive
 @Override
 public void onReceive(Context context, Intent intent) {
   System.out.println("SampleAlarmReceiver on receive");
   try {
     DownloaderClientMarshaller.startDownloadServiceIfRequired(
         context, intent, SampleDownloaderService.class);
   } catch (NameNotFoundException e) {
     e.printStackTrace();
   }
 }
  /**
   * If the download isn't present, we initialize the download UI. This ties all of the controls
   * into the remote service calls.
   */
  private void initializeDownloadUI() {
    mDownloaderClientStub =
        DownloaderClientMarshaller.CreateStub(this, GNURootDownloaderService.class);
    setContentView(R.layout.main_downloader);

    mPB = (ProgressBar) findViewById(R.id.progressBar);
    mStatusText = (TextView) findViewById(R.id.statusText);
    mProgressFraction = (TextView) findViewById(R.id.progressAsFraction);
    mProgressPercent = (TextView) findViewById(R.id.progressAsPercentage);
    mAverageSpeed = (TextView) findViewById(R.id.progressAverageSpeed);
    mTimeRemaining = (TextView) findViewById(R.id.progressTimeRemaining);
    mDashboard = findViewById(R.id.downloaderDashboard);
    mCellMessage = findViewById(R.id.approveCellular);
    mPauseButton = (Button) findViewById(R.id.pauseButton);
    mWiFiSettingsButton = (Button) findViewById(R.id.wifiSettingsButton);

    mPauseButton.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View view) {
            if (mStatePaused) {
              mRemoteService.requestContinueDownload();
            } else {
              mRemoteService.requestPauseDownload();
            }
            setButtonPausedState(!mStatePaused);
          }
        });

    mWiFiSettingsButton.setOnClickListener(
        new View.OnClickListener() {

          @Override
          public void onClick(View v) {
            startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
          }
        });

    Button resumeOnCell = (Button) findViewById(R.id.resumeOverCellular);
    resumeOnCell.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View view) {
            mRemoteService.setDownloadFlags(IDownloaderService.FLAGS_DOWNLOAD_OVER_CELLULAR);
            mRemoteService.requestContinueDownload();
            mCellMessage.setVisibility(View.GONE);
          }
        });
  }
  /**
   * Called when the activity is first create; we wouldn't create a layout in the case where we have
   * the file and are moving to another activity without downloading.
   */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mMe = this;

    /** Both downloading and validation make use of the "download" UI */
    initializeDownloadUI();

    /**
     * Before we do anything, are the files we expect already here and delivered (presumably by
     * Market) For free titles, this is probably worth doing. (so no Market request is necessary)
     */
    if (!expansionFilesDelivered()) {

      try {
        Intent launchIntent = GNURootDownloaderActivity.this.getIntent();
        Intent intentToLaunchThisActivityFromNotification =
            new Intent(GNURootDownloaderActivity.this, GNURootDownloaderActivity.this.getClass());
        intentToLaunchThisActivityFromNotification.setFlags(
            Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intentToLaunchThisActivityFromNotification.setAction(launchIntent.getAction());

        if (launchIntent.getCategories() != null) {
          for (String category : launchIntent.getCategories()) {
            intentToLaunchThisActivityFromNotification.addCategory(category);
          }
        }

        // Build PendingIntent used to open this activity from
        // Notification
        PendingIntent pendingIntent =
            PendingIntent.getActivity(
                GNURootDownloaderActivity.this,
                0,
                intentToLaunchThisActivityFromNotification,
                PendingIntent.FLAG_UPDATE_CURRENT);
        // Request to start the download
        int startResult =
            DownloaderClientMarshaller.startDownloadServiceIfRequired(
                this, pendingIntent, GNURootDownloaderService.class);

        if (startResult != DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED) {
          // The DownloaderService has started downloading the files,
          // show progress
          initializeDownloadUI();
          return;
        } // otherwise, download not needed so we fall through to
        // starting the movie
      } catch (NameNotFoundException e) {
        Log.e(LOG_TAG, "Cannot find own package! MAYDAY!");
        e.printStackTrace();
      }

      //        } else {
      //            validateXAPKZipFiles();
    } else {
      Intent intent = this.getIntent();
      this.setResult(xAPKS[0].mFileVersion, intent);
      finish();
    }
  }
  /**
   * Allow to know if the expansion files was downloaded Always return true in debug mode It's the
   * caller responsibility to setup the download UI if it returns false
   *
   * @return true if the expansion was delivered, false if it's starting to download
   */
  public static boolean checkExpansionFiles() {
    boolean isDebuggable =
        (0
            != (NativeUtility.getMainActivity().getApplicationInfo().flags
                & ApplicationInfo.FLAG_DEBUGGABLE));
    if (isDebuggable) {
      // return true;
    }
    if (xAPKS == null) {
      return true;
    }
    for (XAPKFile xf : xAPKS) {
      String fileName =
          Helpers.getExpansionAPKFileName(
              NativeUtility.getMainActivity(), xf.mIsMain, xf.mFileVersion);
      if (!Helpers.doesFileExist(NativeUtility.getMainActivity(), fileName, xf.mFileSize, false)) {
        // Build an Intent to start this activity from the Notification
        Intent notifierIntent =
            new Intent(NativeUtility.getMainActivity(), NativeUtility.getMainActivity().getClass());
        notifierIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent =
            PendingIntent.getActivity(
                NativeUtility.getMainActivity(),
                0,
                notifierIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        // Start the download service (if required)
        int startResult = -1;
        try {
          startResult =
              DownloaderClientMarshaller.startDownloadServiceIfRequired(
                  NativeUtility.getMainActivity(), pendingIntent, ExpansionSupport.class);
        } catch (NameNotFoundException e) {
          e.printStackTrace();
        }
        // If download has started, initialize this activity to show download progress
        if (startResult != DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED) {
          // The caller will setup the download UI
          Log.i(TAG, "creating stub for download ...");
          NativeUtility.getMainActivity()
              .runOnUiThread(
                  new Runnable() {
                    public void
                        run() { // For some reasons, that needs to be runned on UI Thread because it
                      // requires a Looper ....
                      try {
                        mDownloaderClientStub =
                            DownloaderClientMarshaller.CreateStub(
                                ExpansionSupport.getInstance(), ExpansionSupport.class);
                        if (NativeUtility.getMainActivity().isActive()) {
                          mDownloaderClientStub.connect(NativeUtility.getMainActivity());
                        }
                      } catch (Exception e) {
                        e.printStackTrace();
                      }
                    }
                  });
          Log.i(TAG, "Stub created! Returning false");

          return false;
        }
      }
    }
    Log.i(TAG, "Expansion file exists");
    return true;
  }
  /**
   * Called when the activity is first create; we wouldn't create a layout in the case where we have
   * the file and are moving to another activity without downloading.
   */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GameActivity.Log.debug("Starting DownloaderActivity...");
    _download = this;
    // Create somewhere to place the output - we'll check this on 'finish' to make sure we are
    // returning 'something'
    OutputData = new Intent();

    /** Both downloading and validation make use of the "download" UI */
    initializeDownloadUI();
    GameActivity.Log.debug("... UI setup. Checking for files.");

    /**
     * Before we do anything, are the files we expect already here and delivered (presumably by
     * Market) For free titles, this is probably worth doing. (so no Market request is necessary)
     */
    if (!expansionFilesDelivered()) {
      GameActivity.Log.debug("... Whoops... missing; go go go download system!");

      try {

        // Make sure we have a key before we try to start the service
        if (OBBDownloaderService.getPublicKeyLength() == 0) {
          AlertDialog.Builder builder = new AlertDialog.Builder(this);

          builder
              .setCancelable(false)
              .setTitle("No Google Play Store Key")
              .setMessage(
                  "No OBB found and no store key to try to download. Please set one up in Android Project Settings")
              .setPositiveButton(
                  "Exit",
                  new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int item) {
                      OutputData.putExtra(
                          GameActivity.DOWNLOAD_RETURN_NAME, GameActivity.DOWNLOAD_NO_PLAY_KEY);
                      setResult(RESULT_OK, OutputData);
                      finish();
                    }
                  });

          AlertDialog alert = builder.create();
          alert.show();
        } else {

          Intent launchIntent = DownloaderActivity.this.getIntent();
          Intent intentToLaunchThisActivityFromNotification =
              new Intent(DownloaderActivity.this, DownloaderActivity.this.getClass());
          intentToLaunchThisActivityFromNotification.setFlags(
              Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
          intentToLaunchThisActivityFromNotification.setAction(launchIntent.getAction());

          if (launchIntent.getCategories() != null) {
            for (String category : launchIntent.getCategories()) {
              intentToLaunchThisActivityFromNotification.addCategory(category);
            }
          }

          // Build PendingIntent used to open this activity from
          // Notification
          PendingIntent pendingIntent =
              PendingIntent.getActivity(
                  DownloaderActivity.this,
                  0,
                  intentToLaunchThisActivityFromNotification,
                  PendingIntent.FLAG_UPDATE_CURRENT);
          // Request to start the download
          int startResult =
              DownloaderClientMarshaller.startDownloadServiceIfRequired(
                  this, pendingIntent, OBBDownloaderService.class);

          if (startResult != DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED) {
            // The DownloaderService has started downloading the files,
            // show progress
            initializeDownloadUI();
            return;
          } // otherwise, download not needed so we fall through to saying all is OK
          else {
            OutputData.putExtra(
                GameActivity.DOWNLOAD_RETURN_NAME, GameActivity.DOWNLOAD_FILES_PRESENT);
            setResult(RESULT_OK, OutputData);
            finish();
          }
        }

      } catch (NameNotFoundException e) {
        Log.e(LOG_TAG, "Cannot find own package! MAYDAY!");
        e.printStackTrace();
      }

    } else {
      GameActivity.Log.debug("... Can has! Check 'em Dano!");
      if (!onlySingleExpansionFileFound()) {
        // Do some UI here to figure out which we want to keep

        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder
            .setCancelable(false)
            .setTitle("Select OBB to use")
            .setItems(
                OBBSelectItems,
                new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int item) {
                    DownloaderActivity.RemoveOBBFile(item);
                    ProcessOBBFiles();
                  }
                });

        AlertDialog alert = builder.create();
        alert.show();
      } else {
        ProcessOBBFiles();
      }
    }
  }
Exemple #7
0
  @Override
  protected void onCreate(Bundle icicle) {

    Log.d("GODOT", "** GODOT ACTIVITY CREATED HERE ***\n");

    super.onCreate(icicle);
    _self = this;
    Window window = getWindow();
    window.addFlags(
        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    // check for apk expansion API
    if (true) {
      boolean md5mismatch = false;
      command_line = getCommandLine();
      boolean use_apk_expansion = false;
      String main_pack_md5 = null;
      String main_pack_key = null;

      List<String> new_args = new LinkedList<String>();

      for (int i = 0; i < command_line.length; i++) {

        boolean has_extra = i < command_line.length - 1;
        if (command_line[i].equals("-use_apk_expansion")) {
          use_apk_expansion = true;
        } else if (has_extra && command_line[i].equals("-apk_expansion_md5")) {
          main_pack_md5 = command_line[i + 1];
          i++;
        } else if (has_extra && command_line[i].equals("-apk_expansion_key")) {
          main_pack_key = command_line[i + 1];
          SharedPreferences prefs = getSharedPreferences("app_data_keys", MODE_PRIVATE);
          Editor editor = prefs.edit();
          editor.putString("store_public_key", main_pack_key);

          editor.commit();
          i++;
        } else if (command_line[i].trim().length() != 0) {
          new_args.add(command_line[i]);
        }
      }

      if (new_args.isEmpty()) {
        command_line = null;
      } else {

        command_line = new_args.toArray(new String[new_args.size()]);
      }
      if (use_apk_expansion && main_pack_md5 != null && main_pack_key != null) {
        // check that environment is ok!
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
          Log.d("GODOT", "**ERROR! No media mounted!");
          // show popup and die
        }

        // Build the full path to the app's expansion files
        try {
          expansion_pack_path =
              Environment.getExternalStorageDirectory().toString()
                  + "/Android/obb/"
                  + this.getPackageName();
          expansion_pack_path +=
              "/"
                  + "main."
                  + getPackageManager().getPackageInfo(getPackageName(), 0).versionCode
                  + "."
                  + this.getPackageName()
                  + ".obb";
        } catch (Exception e) {
          e.printStackTrace();
        }

        File f = new File(expansion_pack_path);

        boolean pack_valid = true;
        Log.d("GODOT", "**PACK** - Path " + expansion_pack_path);

        if (!f.exists()) {

          pack_valid = false;
          Log.d("GODOT", "**PACK** - File does not exist");

        } else if (obbIsCorrupted(expansion_pack_path, main_pack_md5)) {
          Log.d("GODOT", "**PACK** - Expansion pack (obb) is corrupted");
          pack_valid = false;
          try {
            f.delete();
          } catch (Exception e) {
            Log.d("GODOT", "**PACK** - Error deleting corrupted expansion pack (obb)");
          }
        }

        if (!pack_valid) {
          Log.d("GODOT", "Pack Invalid, try re-downloading.");

          Intent notifierIntent = new Intent(this, this.getClass());
          notifierIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

          PendingIntent pendingIntent =
              PendingIntent.getActivity(this, 0, notifierIntent, PendingIntent.FLAG_UPDATE_CURRENT);

          int startResult;
          try {
            Log.d("GODOT", "INITIALIZING DOWNLOAD");
            startResult =
                DownloaderClientMarshaller.startDownloadServiceIfRequired(
                    getApplicationContext(), pendingIntent, GodotDownloaderService.class);
            Log.d("GODOT", "DOWNLOAD SERVICE FINISHED:" + startResult);

            if (startResult != DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED) {
              Log.d("GODOT", "DOWNLOAD REQUIRED");
              // This is where you do set up to display the download
              // progress (next step)
              mDownloaderClientStub =
                  DownloaderClientMarshaller.CreateStub(this, GodotDownloaderService.class);

              setContentView(com.godot.game.R.layout.downloading_expansion);
              mPB = (ProgressBar) findViewById(com.godot.game.R.id.progressBar);
              mStatusText = (TextView) findViewById(com.godot.game.R.id.statusText);
              mProgressFraction = (TextView) findViewById(com.godot.game.R.id.progressAsFraction);
              mProgressPercent = (TextView) findViewById(com.godot.game.R.id.progressAsPercentage);
              mAverageSpeed = (TextView) findViewById(com.godot.game.R.id.progressAverageSpeed);
              mTimeRemaining = (TextView) findViewById(com.godot.game.R.id.progressTimeRemaining);
              mDashboard = findViewById(com.godot.game.R.id.downloaderDashboard);
              mCellMessage = findViewById(com.godot.game.R.id.approveCellular);
              mPauseButton = (Button) findViewById(com.godot.game.R.id.pauseButton);
              mWiFiSettingsButton = (Button) findViewById(com.godot.game.R.id.wifiSettingsButton);

              return;
            } else {
              Log.d("GODOT", "NO DOWNLOAD REQUIRED");
            }
          } catch (NameNotFoundException e) {
            // TODO Auto-generated catch block
            Log.d("GODOT", "Error downloading expansion package:" + e.getMessage());
          }
        }
      }
    }

    initializeGodot();

    //	instanceSingleton( new GodotFacebook(this) );

  }