|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright 2014 Rafael Garcia Moreno. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + ******************************************************************************/ |
| 16 | +package com.bladecoder.engineeditor.ui; |
| 17 | + |
| 18 | +import java.util.Arrays; |
| 19 | + |
| 20 | +import com.badlogic.gdx.scenes.scene2d.ui.Skin; |
| 21 | +import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; |
| 22 | +import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener.ChangeEvent; |
| 23 | +import com.bladecoder.engineeditor.common.Message; |
| 24 | +import com.bladecoder.engineeditor.common.RunProccess; |
| 25 | +import com.bladecoder.engineeditor.ui.panels.EditDialog; |
| 26 | +import com.bladecoder.engineeditor.ui.panels.FileInputPanel; |
| 27 | +import com.bladecoder.engineeditor.ui.panels.InputPanel; |
| 28 | +import com.bladecoder.engineeditor.ui.panels.InputPanelFactory; |
| 29 | + |
| 30 | +public class CreateAndroidKeystoreDialog extends EditDialog { |
| 31 | + |
| 32 | + private static final String INFO = "Create the keystore needed to sign Android packages."; |
| 33 | + |
| 34 | + private InputPanel keyStoreFile; |
| 35 | + private InputPanel androidKeyAlias; |
| 36 | + private InputPanel androidKeyStorePassword; |
| 37 | + private InputPanel androidKeyAliasPassword; |
| 38 | + |
| 39 | + protected ChangeListener listener; |
| 40 | + |
| 41 | + public CreateAndroidKeystoreDialog(Skin skin) { |
| 42 | + super("CREATE KEY STORE FOR ANDROID", skin); |
| 43 | + |
| 44 | + keyStoreFile = new FileInputPanel(skin, "Select the key store", "Select the key store file name and location", |
| 45 | + FileInputPanel.DialogType.SAVE_FILE); |
| 46 | + |
| 47 | + androidKeyAlias = InputPanelFactory.createInputPanel(skin, "KeyAlias", "Select the Key ID/Alias", true); |
| 48 | + |
| 49 | + androidKeyStorePassword = InputPanelFactory.createInputPanel(skin, "KeyStorePasswd", "Key Store Password", |
| 50 | + true); |
| 51 | + androidKeyAliasPassword = InputPanelFactory.createInputPanel(skin, "KeyAliasPasswd", "Key Alias Password", |
| 52 | + true); |
| 53 | + |
| 54 | + addInputPanel(keyStoreFile); |
| 55 | + addInputPanel(androidKeyAlias); |
| 56 | + addInputPanel(androidKeyStorePassword); |
| 57 | + addInputPanel(androidKeyAliasPassword); |
| 58 | + |
| 59 | + setInfo(INFO); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected void ok() { |
| 64 | + createKeyStore(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected boolean validateFields() { |
| 69 | + boolean ok = true; |
| 70 | + |
| 71 | + if (!keyStoreFile.validateField()) |
| 72 | + ok = false; |
| 73 | + |
| 74 | + if (!androidKeyAlias.validateField()) |
| 75 | + ok = false; |
| 76 | + |
| 77 | + if (androidKeyStorePassword.getText() == null || androidKeyStorePassword.getText().length() < 6) { |
| 78 | + Message.showMsgDialog(getStage(), "Error", "Keystore password must be at least 6 character long"); |
| 79 | + ok = false; |
| 80 | + |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + if (androidKeyAliasPassword.getText() == null || androidKeyAliasPassword.getText().length() < 6) { |
| 85 | + Message.showMsgDialog(getStage(), "Error", "Key password must be at least 6 character long"); |
| 86 | + ok = false; |
| 87 | + } |
| 88 | + |
| 89 | + return ok; |
| 90 | + } |
| 91 | + |
| 92 | + private void createKeyStore() { |
| 93 | + // keytool -genkey -v -keystore my-release-key.keystore -alias alias_name |
| 94 | + // -keyalg RSA -keysize 2048 -validity 10000 |
| 95 | + |
| 96 | + String[] args = { "-genkey", "-noprompt", "-v", "-keystore", getKeyStorePath(), "-alias", getKeyAlias(), |
| 97 | + "-keyalg", "RSA", "-keysize", "2048", "-validity", "10000", "-storepass", getKeyStorePassword(), |
| 98 | + "-dname", "CN=bladeengine.com", "-keypass", getKeyAliasPassword() }; |
| 99 | + |
| 100 | + try { |
| 101 | + Process p = RunProccess.runJavaHomeBin("keytool", Arrays.asList(args)); |
| 102 | + p.waitFor(); |
| 103 | + |
| 104 | + if (p.exitValue() == 0) { |
| 105 | + if (listener != null) |
| 106 | + listener.changed(new ChangeEvent(), this); |
| 107 | + } else { |
| 108 | + Message.showMsgDialog(getStage(), "Error", "Error generating key"); |
| 109 | + cancel(); |
| 110 | + } |
| 111 | + } catch (Exception e) { |
| 112 | + Message.showMsgDialog(getStage(), "Error", e.getMessage()); |
| 113 | + cancel(); |
| 114 | + } |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + public String getKeyStorePath() { |
| 119 | + return keyStoreFile.getText(); |
| 120 | + } |
| 121 | + |
| 122 | + public String getKeyAlias() { |
| 123 | + return androidKeyAlias.getText(); |
| 124 | + } |
| 125 | + |
| 126 | + public String getKeyStorePassword() { |
| 127 | + return androidKeyStorePassword.getText(); |
| 128 | + } |
| 129 | + |
| 130 | + public String getKeyAliasPassword() { |
| 131 | + return androidKeyAliasPassword.getText(); |
| 132 | + } |
| 133 | + |
| 134 | + public void setListener(ChangeListener l) { |
| 135 | + listener = l; |
| 136 | + } |
| 137 | +} |
0 commit comments