// Copyright 2022 Google Inc.
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//      http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.android_quick_settings;

import android.app.StatusBarManager;
import android.content.ComponentName;
import android.os.Build.VERSION;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.core.graphics.drawable.IconCompat;
import com.google.common.util.concurrent.MoreExecutors;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    StatusBarManager statusBarService = this.getSystemService(StatusBarManager.class);
    ComponentName componentName = new ComponentName(
        QuickSettingsService.class.getPackage().getName(),
        QuickSettingsService.class.getName());
    AppCompatButton btn = findViewById(R.id.add_tile_btn);
    IconCompat icon =
        IconCompat.createWithResource(getApplicationContext(),
            android.R.drawable.ic_dialog_alert);
    btn.setOnClickListener(view -> {
      if (VERSION.SDK_INT >= 33) {
        statusBarService.requestAddTileService(
            componentName, "Quick Settings", icon.toIcon(MainActivity.this),
            MoreExecutors.directExecutor(), integer -> {
              setResult(integer);
              finish();
            });
      } else {
        Logger.getLogger(MainActivity.class.getName()).log(
            Level.INFO, "Request to add tile for user is not supported");
      }
    });
  }
}

@@ -764,16 +764,85 @@
 Bundle;%0A
+import android.widget.Toast;%0Aimport androidx.annotation.RequiresApi;%0A
 import a
@@ -882,16 +882,16 @@
 tivity;%0A
-
 import a
@@ -1160,16 +1160,41 @@
 vity %7B%0A%0A
+  @RequiresApi(api = 33)%0A
   @Overr
@@ -1489,55 +1489,34 @@
     
-QuickSettingsService.class.getPackage().getName
+this.getApplicationContext
 (),%0A
@@ -1755,16 +1755,17 @@
 alert);%0A
+%0A
     btn.
@@ -1815,21 +1815,45 @@
 ION.
-SDK_INT %3E= 33
+CODENAME.equalsIgnoreCase(%22Tiramisu%22)
 ) %7B%0A
@@ -2265,16 +2265,16 @@
       %7D%0A
-
     %7D);%0A
@@ -2276,11 +2276,1328 @@
  %7D);%0A  %7D
+%0A%0A  private String resultToString(int resultCode) %7B%0A    String value;%0A    switch(resultCode) %7B%0A      case StatusBarManager.TILE_ADD_REQUEST_RESULT_TILE_NOT_ADDED:%0A        value = %22Tile not added%22;%0A        break;%0A      case StatusBarManager.TILE_ADD_REQUEST_RESULT_TILE_ALREADY_ADDED:%0A        value = %22Tile already added%22;%0A        break;%0A      case StatusBarManager.TILE_ADD_REQUEST_RESULT_TILE_ADDED:%0A        value = %22Tile added%22;%0A        break;%0A      case StatusBarManager.TILE_ADD_REQUEST_ERROR_MISMATCHED_PACKAGE:%0A        value = %22Error: mismatched package%22;%0A        break;%0A      case StatusBarManager.TILE_ADD_REQUEST_ERROR_REQUEST_IN_PROGRESS:%0A        value = %22Error: request in progress%22;%0A        break;%0A      case StatusBarManager.TILE_ADD_REQUEST_ERROR_BAD_COMPONENT:%0A        value = %22Error: bad component name%22;%0A        break;%0A      case StatusBarManager.TILE_ADD_REQUEST_ERROR_NOT_CURRENT_USER:%0A        value = %22Error: not current user%22;%0A        break;%0A      case StatusBarManager.TILE_ADD_REQUEST_ERROR_APP_NOT_IN_FOREGROUND:%0A        value = %22Error: app not in foreground%22;%0A        break;%0A      case StatusBarManager.TILE_ADD_REQUEST_ERROR_NO_STATUS_BAR_SERVICE:%0A        value = %22Error: no statusbar service found%22;%0A        break;%0A      default:%0A        value = %22Unknown error%22;%0A    %7D%0A    return value;%0A  %7D
 %0A%7D%0A

fix API param passed