| /* |
| * Copyright (C) 2017 The Android Open Source Project |
| * |
| * 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.appindexing.ui; |
| |
| import com.android.tools.adtui.util.FormScalingUtil; |
| import com.google.appindexing.fetchasgoogle.FetchAsGoogleClient; |
| import com.google.appindexing.fetchasgoogle.FetchAsGoogleTask; |
| import com.google.appindexing.util.AppIndexingBundle; |
| import com.google.gct.login.GoogleLogin; |
| import com.intellij.icons.AllIcons; |
| import com.intellij.ide.BrowserUtil; |
| import com.intellij.openapi.application.ApplicationManager; |
| import com.intellij.openapi.application.ModalityState; |
| import com.intellij.openapi.diagnostic.Logger; |
| import com.intellij.openapi.module.Module; |
| import com.intellij.openapi.module.ModuleManager; |
| import com.intellij.openapi.module.ModuleType; |
| import com.intellij.openapi.project.Project; |
| import com.intellij.openapi.ui.DialogWrapper; |
| import com.intellij.ui.ListCellRendererWrapper; |
| import com.intellij.ui.SortedComboBoxModel; |
| import org.jetbrains.annotations.NotNull; |
| import org.jetbrains.annotations.Nullable; |
| |
| import javax.swing.*; |
| |
| import java.util.Arrays; |
| import java.util.stream.Collector; |
| |
| import static com.google.appindexing.ui.AppIndexingDialog.isModuleAccepted; |
| |
| /** |
| * A dialog for users to select a module and login (if necessary) before checking app indexing errors. |
| */ |
| public final class CheckErrorDialog extends DialogWrapper { |
| private static final String PERSONAL_CONTENT_HELP_URL = "https://firebase.google.com/docs/app-indexing/android/personal-content"; |
| private static final String USER_ACTION_HELP_URL = "https://firebase.google.com/docs/app-indexing/android/log-actions"; |
| |
| private JPanel myPanel; |
| private JComboBox<Module> myModulesComboBox; |
| private JPanel myWarningPanel; |
| private JLabel myWarningTitle; |
| private JLabel myWarningLabel; |
| private JButton myLoginButton; |
| private Project myProject; |
| private FetchAsGoogleTask.RequestType myRequestType; |
| |
| private static Logger getLog() { |
| return Logger.getInstance(CheckErrorDialog.class); |
| } |
| |
| private final SortedComboBoxModel<Module> myModules = new SortedComboBoxModel<>( |
| (module, module1) -> module.getName().compareToIgnoreCase(module1.getName())); |
| |
| public CheckErrorDialog(@NotNull Project project, FetchAsGoogleTask.RequestType requestType) { |
| super(project); |
| FormScalingUtil.scaleComponentTree(this.getClass(), myPanel); |
| init(); |
| setTitle(AppIndexingBundle.message("app.indexing.check.error.dialog.title")); |
| myProject = project; |
| Module[] modules = ModuleManager.getInstance(project).getModules(); |
| Arrays.stream(modules).filter(AppIndexingDialog::isModuleAccepted).forEach(myModules::add); |
| if (myModules.getSize() != 0) { |
| myModules.setSelectedItem(myModules.getElementAt(0)); |
| } |
| myModulesComboBox.setModel(myModules); |
| myModulesComboBox.setRenderer(new ListCellRendererWrapper<Module>() { |
| @Override |
| public void customize(final JList list, final Module value, final int index, final boolean selected, final boolean hasFocus) { |
| if (value != null) { |
| setIcon(ModuleType.get(value).getIcon()); |
| setText(value.getName()); |
| } |
| } |
| }); |
| myRequestType = requestType; |
| |
| myLoginButton.addActionListener(e -> { |
| try { |
| myLoginButton.setEnabled(false); |
| GoogleLogin.promptToLogIn(null, () -> ApplicationManager.getApplication().invokeLater(this::verifyInput, ModalityState.any())); |
| } |
| catch (Exception ex) { |
| getLog().warn("Failed to login with your Google developer account.", ex); |
| } |
| }); |
| |
| myWarningTitle.setIcon(AllIcons.General.BalloonError); |
| } |
| |
| @Nullable |
| @Override |
| protected JComponent createCenterPanel() { |
| return myPanel; |
| } |
| |
| @Override |
| protected void doHelpAction() { |
| BrowserUtil.browse( |
| myRequestType == FetchAsGoogleTask.RequestType.USER_ACTION_LOGGING_DEBUGGING ? USER_ACTION_HELP_URL : PERSONAL_CONTENT_HELP_URL); |
| } |
| |
| private boolean verifyInput() { |
| myWarningLabel.setText(""); |
| myLoginButton.setVisible(false); |
| myWarningPanel.setVisible(false); |
| if (myModules.getSelectedItem() == null) { |
| myWarningLabel.setText(AppIndexingBundle.message("app.indexing.module.not.provided.warning.message")); |
| myWarningPanel.setVisible(true); |
| return false; |
| } |
| if (!GoogleLogin.getInstance().isLoggedIn()) { |
| myWarningLabel.setText(AppIndexingBundle.message("app.indexing.login.warning.message")); |
| myLoginButton.setVisible(true); |
| myLoginButton.setEnabled(true); |
| myWarningPanel.setVisible(true); |
| return false; |
| } |
| return true; |
| } |
| |
| @Override |
| protected void doOKAction() { |
| if (!verifyInput()) { |
| return; |
| } |
| try { |
| FetchAsGoogleTask task = |
| FetchAsGoogleTask.createFetchAsGoogleTask(myProject, myModules.getSelectedItem(), "", false, myRequestType); |
| ApplicationManager.getApplication().executeOnPooledThread(task); |
| super.doOKAction(); |
| } |
| catch (FetchAsGoogleClient.FetchAsGoogleException ex) { |
| // This should happen very unlikely. |
| getLog().warn(ex); |
| } |
| } |
| } |