コード例 #1
0
  private CrashRecoveryHandler(Controller controller) {
    mController = controller;
    mContext = mController.getActivity().getApplicationContext();
    mForegroundHandler = new Handler();
    mBackgroundHandler =
        new Handler(BackgroundHandler.getLooper()) {

          @Override
          public void handleMessage(Message msg) {
            switch (msg.what) {
              case MSG_WRITE_STATE:
                if (LOGV_ENABLED) {
                  Log.v(LOGTAG, "Saving crash recovery state");
                }
                Parcel p = Parcel.obtain();
                try {
                  Bundle state = (Bundle) msg.obj;
                  state.writeToParcel(p, 0);
                  File stateJournal = new File(mContext.getCacheDir(), STATE_FILE + ".journal");
                  FileOutputStream fout = new FileOutputStream(stateJournal);
                  fout.write(p.marshall());
                  fout.close();
                  File stateFile = new File(mContext.getCacheDir(), STATE_FILE);
                  if (!stateJournal.renameTo(stateFile)) {
                    // Failed to rename, try deleting the existing
                    // file and try again
                    stateFile.delete();
                    stateJournal.renameTo(stateFile);
                  }
                } catch (Throwable e) {
                  Log.i(LOGTAG, "Failed to save persistent state", e);
                } finally {
                  p.recycle();
                }
                break;
              case MSG_CLEAR_STATE:
                if (LOGV_ENABLED) {
                  Log.v(LOGTAG, "Clearing crash recovery state");
                }
                File state = new File(mContext.getCacheDir(), STATE_FILE);
                if (state.exists()) {
                  state.delete();
                }
                break;
              case MSG_PRELOAD_STATE:
                mRecoveryState = loadCrashState();
                synchronized (CrashRecoveryHandler.this) {
                  mIsPreloading = false;
                  mDidPreload = true;
                  CrashRecoveryHandler.this.notifyAll();
                }
                break;
            }
          }
        };
  }
コード例 #2
0
 public UrlHandler(Controller controller) {
   mController = controller;
   mActivity = mController.getActivity();
 }