Пример #1
0
  /**
   * Handle all necessary tasks that can be delayed until initialization completes.
   *
   * @param activityCreationTimeMs The time of creation for the activity this toolbar belongs to.
   * @param activityName Simple class name for the activity this toolbar belongs to.
   */
  public void onDeferredStartup(final long activityCreationTimeMs, final String activityName) {
    // Record startup performance statistics
    long elapsedTime = SystemClock.elapsedRealtime() - activityCreationTimeMs;
    if (elapsedTime < RECORD_UMA_PERFORMANCE_METRICS_DELAY_MS) {
      ThreadUtils.postOnUiThreadDelayed(
          new Runnable() {
            @Override
            public void run() {
              onDeferredStartup(activityCreationTimeMs, activityName);
            }
          },
          RECORD_UMA_PERFORMANCE_METRICS_DELAY_MS - elapsedTime);
    }
    RecordHistogram.recordTimesHistogram(
        "MobileStartup.ToolbarFirstDrawTime." + activityName,
        mToolbar.getFirstDrawTime() - activityCreationTimeMs,
        TimeUnit.MILLISECONDS);

    long firstFocusTime = mToolbar.getLocationBar().getFirstUrlBarFocusTime();
    if (firstFocusTime != 0) {
      RecordHistogram.recordCustomTimesHistogram(
          "MobileStartup.ToolbarFirstFocusTime." + activityName,
          firstFocusTime - activityCreationTimeMs,
          MIN_FOCUS_TIME_FOR_UMA_HISTOGRAM_MS,
          MAX_FOCUS_TIME_FOR_UMA_HISTOGRAM_MS,
          TimeUnit.MILLISECONDS,
          50);
    }
  }
  @Override
  public Notification build() {
    // A note about RemoteViews and updating notifications. When a notification is passed to the
    // {@code NotificationManager} with the same tag and id as a previous notification, an
    // in-place update will be performed. In that case, the actions of all new
    // {@link RemoteViews} will be applied to the views of the old notification. This is safe
    // for actions that overwrite old values such as setting the text of a {@code TextView}, but
    // care must be taken for additive actions. Especially in the case of
    // {@link RemoteViews#addView} the result could be to append new views below stale ones. In
    // that case {@link RemoteViews#removeAllViews} must be called before adding new ones.
    RemoteViews compactView = new RemoteViews(mContext.getPackageName(), R.layout.web_notification);
    RemoteViews bigView = new RemoteViews(mContext.getPackageName(), R.layout.web_notification_big);

    float fontScale = mContext.getResources().getConfiguration().fontScale;
    bigView.setInt(R.id.body, "setMaxLines", calculateMaxBodyLines(fontScale));
    int scaledPadding =
        calculateScaledPadding(fontScale, mContext.getResources().getDisplayMetrics());
    String formattedTime = "";

    // Temporarily allowing disk access. TODO: Fix. See http://crbug.com/577185
    StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
    StrictMode.allowThreadDiskWrites();
    try {
      long time = SystemClock.elapsedRealtime();
      formattedTime = DateFormat.getTimeFormat(mContext).format(new Date());
      RecordHistogram.recordTimesHistogram(
          "Android.StrictMode.NotificationUIBuildTime",
          SystemClock.elapsedRealtime() - time,
          TimeUnit.MILLISECONDS);
    } finally {
      StrictMode.setThreadPolicy(oldPolicy);
    }

    for (RemoteViews view : new RemoteViews[] {compactView, bigView}) {
      view.setTextViewText(R.id.time, formattedTime);
      view.setTextViewText(R.id.title, mTitle);
      view.setTextViewText(R.id.body, mBody);
      view.setTextViewText(R.id.origin, mOrigin);
      view.setImageViewBitmap(R.id.icon, getNormalizedLargeIcon());
      view.setViewPadding(R.id.title, 0, scaledPadding, 0, 0);
      view.setViewPadding(R.id.body_container, 0, scaledPadding, 0, scaledPadding);
      addWorkProfileBadge(view);

      int smallIconId = useMaterial() ? R.id.small_icon_overlay : R.id.small_icon_footer;
      view.setViewVisibility(smallIconId, View.VISIBLE);
      if (mSmallIconBitmap != null) {
        view.setImageViewBitmap(smallIconId, mSmallIconBitmap);
      } else {
        view.setImageViewResource(smallIconId, mSmallIconId);
      }
    }
    addActionButtons(bigView);
    configureSettingsButton(bigView);

    // Note: this is not a NotificationCompat builder so be mindful of the
    // API level of methods you call on the builder.
    Notification.Builder builder = new Notification.Builder(mContext);
    builder.setTicker(mTickerText);
    builder.setContentIntent(mContentIntent);
    builder.setDeleteIntent(mDeleteIntent);
    builder.setDefaults(mDefaults);
    builder.setVibrate(mVibratePattern);
    builder.setWhen(mTimestamp);
    builder.setOnlyAlertOnce(!mRenotify);
    builder.setContent(compactView);

    // Some things are duplicated in the builder to ensure the notification shows correctly on
    // Wear devices and custom lock screens.
    builder.setContentTitle(mTitle);
    builder.setContentText(mBody);
    builder.setSubText(mOrigin);
    builder.setLargeIcon(getNormalizedLargeIcon());
    setSmallIconOnBuilder(builder, mSmallIconId, mSmallIconBitmap);
    for (Action action : mActions) {
      addActionToBuilder(builder, action);
    }
    if (mSettingsAction != null) {
      addActionToBuilder(builder, mSettingsAction);
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      // Notification.Builder.setPublicVersion was added in Android L.
      builder.setPublicVersion(createPublicNotification(mContext));
    }

    Notification notification = builder.build();
    notification.bigContentView = bigView;
    return notification;
  }