|
| 1 | +package org.qpython.qpy.main.activity; |
| 2 | + |
| 3 | +import android.content.ActivityNotFoundException; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.Intent; |
| 6 | +import android.content.pm.PackageManager; |
| 7 | +import android.net.Uri; |
| 8 | +import android.os.Build; |
| 9 | +import android.os.Bundle; |
| 10 | +import android.os.Environment; |
| 11 | +import android.support.annotation.NonNull; |
| 12 | +import android.support.annotation.Nullable; |
| 13 | +import android.support.v4.app.ActivityCompat; |
| 14 | +import android.support.v4.content.ContextCompat; |
| 15 | +import android.support.v4.util.ArrayMap; |
| 16 | +import android.support.v7.app.AppCompatActivity; |
| 17 | +import android.text.TextUtils; |
| 18 | +import android.widget.Toast; |
| 19 | + |
| 20 | +import com.quseit.util.NAction; |
| 21 | +import com.quseit.util.NUtil; |
| 22 | +import com.umeng.analytics.MobclickAgent; |
| 23 | + |
| 24 | +import org.qpython.qpy.R; |
| 25 | +import org.qpython.qpy.console.ShellTermSession; |
| 26 | +import org.qpython.qpy.console.util.TermSettings; |
| 27 | +import org.qpython.qpy.main.server.gist.TokenManager; |
| 28 | +import org.renpy.android.ResourceManager; |
| 29 | + |
| 30 | +import java.io.File; |
| 31 | +import java.io.FileInputStream; |
| 32 | +import java.io.FileNotFoundException; |
| 33 | +import java.io.FileOutputStream; |
| 34 | +import java.io.IOException; |
| 35 | +import java.io.InputStream; |
| 36 | +import java.io.OutputStream; |
| 37 | +import java.text.MessageFormat; |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.Map; |
| 40 | + |
| 41 | + |
| 42 | +public class BaseActivity extends AppCompatActivity { |
| 43 | + // QPython interfaces |
| 44 | + private static final int SCRIPT_CONSOLE_CODE = 1237; |
| 45 | + private static final int PID_INIT_VALUE = -1; |
| 46 | + private static final int DEFAULT_BUFFER_SIZE = 8192; |
| 47 | + private static final int LOG_NOTIFICATION_ID = (int) System.currentTimeMillis(); |
| 48 | + private ArrayList<String> mArguments = new ArrayList<>(); |
| 49 | + private InputStream mIn; |
| 50 | + private OutputStream mOut; |
| 51 | + private Map<Integer, PermissionAction> mActionMap = new ArrayMap<>(); |
| 52 | + private TermSettings mSettings; |
| 53 | + private ShellTermSession session; |
| 54 | + |
| 55 | + private boolean permissionGrant = true; |
| 56 | + |
| 57 | + protected static ShellTermSession createTermSession(Context context, TermSettings settings, String initialCommand, String path) { |
| 58 | + ShellTermSession session = null; |
| 59 | + try { |
| 60 | + session = new ShellTermSession(context, settings, initialCommand, path); |
| 61 | + session.setProcessExitMessage(context.getString(R.string.process_exit_message)); |
| 62 | + |
| 63 | + } catch (IOException e) { |
| 64 | + e.printStackTrace(); |
| 65 | + } |
| 66 | + |
| 67 | + return session; |
| 68 | + } |
| 69 | + |
| 70 | + protected void toast(String content) { |
| 71 | + Toast.makeText(this, content, Toast.LENGTH_SHORT).show(); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + protected void onResume() { |
| 76 | + super.onResume(); |
| 77 | + MobclickAgent.onPageStart(this.getLocalClassName()); |
| 78 | + MobclickAgent.onResume(this); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 83 | + super.onCreate(savedInstanceState); |
| 84 | + MobclickAgent.setDebugMode(true); |
| 85 | + MobclickAgent.openActivityDurationTrack(false); |
| 86 | + MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + protected void onPause() { |
| 91 | + super.onPause(); |
| 92 | + MobclickAgent.onPageEnd(this.getLocalClassName()); |
| 93 | + MobclickAgent.onPause(this); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { |
| 98 | + PermissionAction action = mActionMap.get(requestCode); |
| 99 | + if (action != null) { |
| 100 | + if (grantResults.length > 0 && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) { |
| 101 | + action.onGrant(); |
| 102 | + } else { |
| 103 | + action.onDeny(); |
| 104 | + } |
| 105 | + |
| 106 | + } |
| 107 | + mActionMap.remove(action); |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + public final void checkPermissionDo(String[] permissions, PermissionAction action) { |
| 112 | + if (Build.VERSION.SDK_INT >= 23) { |
| 113 | + boolean granted = true; |
| 114 | + for (String permission : permissions) { |
| 115 | + int checkPermission = ContextCompat.checkSelfPermission(this, permission); |
| 116 | + granted = checkPermission == PackageManager.PERMISSION_GRANTED; |
| 117 | + } |
| 118 | + if (!granted) { |
| 119 | + int code = permissions.hashCode() & 0xffff; |
| 120 | + mActionMap.put(code, action); |
| 121 | + ActivityCompat.requestPermissions(this, permissions, code); |
| 122 | + } else { |
| 123 | + action.onGrant(); |
| 124 | + } |
| 125 | + } else { |
| 126 | + action.onGrant(); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + // feedback |
| 132 | + public void onFeedback(String feedback) { |
| 133 | + |
| 134 | + String app = getString(R.string.app_name); |
| 135 | + int ver = NUtil.getVersinoCode(getApplicationContext()); |
| 136 | + String subject = MessageFormat.format(getString(com.quseit.android.R.string.feeback_email_title), app, ver, Build.PRODUCT); |
| 137 | + |
| 138 | + String lastError = ""; |
| 139 | + String code = NAction.getCode(getApplicationContext()); |
| 140 | + File log = new File(Environment.getExternalStorageDirectory() + "/" + code + "_last_err.log"); |
| 141 | + if (log.exists()) { |
| 142 | + lastError = com.quseit.util.FileHelper.getFileContents(log.getAbsolutePath()); |
| 143 | + } |
| 144 | + |
| 145 | + String body = MessageFormat.format(getString(R.string.feedback_email_body), Build.PRODUCT, |
| 146 | + Build.VERSION.RELEASE, Build.VERSION.SDK, lastError, feedback); |
| 147 | + |
| 148 | + Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" + getString(R.string.ui_feedback_mail))); |
| 149 | + |
| 150 | + intent.putExtra(Intent.EXTRA_SUBJECT, subject); |
| 151 | + intent.putExtra(Intent.EXTRA_TEXT, session == null ? body : session.getTranscriptText().trim()); |
| 152 | + try { |
| 153 | + startActivity(Intent.createChooser(intent, |
| 154 | + getString(R.string.email_transcript_chooser_title))); |
| 155 | + } catch (ActivityNotFoundException e) { |
| 156 | + Toast.makeText(this, |
| 157 | + R.string.email_transcript_no_email_activity_found, |
| 158 | + Toast.LENGTH_LONG) |
| 159 | + .show(); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + protected boolean checkExpired(final String resource, String filesDir, String tag) { |
| 164 | + ResourceManager resourceManager = new ResourceManager(this); |
| 165 | + |
| 166 | + String data_version = resourceManager.getString(resource + "_version"); |
| 167 | + String disk_version = "0"; |
| 168 | + |
| 169 | + // If no version, no unpacking is necessary. |
| 170 | + if (data_version == null) { |
| 171 | + return false; |
| 172 | + } |
| 173 | + |
| 174 | + // Check the current disk version, if any. |
| 175 | + String disk_version_fn = filesDir + "/lib/" + tag + "_" + resource + ".version"; |
| 176 | + |
| 177 | + try { |
| 178 | + byte buf[] = new byte[64]; |
| 179 | + InputStream is = new FileInputStream(disk_version_fn); |
| 180 | + int len = is.read(buf); |
| 181 | + disk_version = new String(buf, 0, len); |
| 182 | + is.close(); |
| 183 | + } catch (Exception e) { |
| 184 | + |
| 185 | + disk_version = "0"; |
| 186 | + //Mint.logException(e); |
| 187 | + |
| 188 | + } |
| 189 | + |
| 190 | + //LogUtil.d(TAG, "data_version:"+Math.round(Double.parseDouble(data_version))+"-disk_version:"+Math.round(Double.parseDouble(disk_version))+"-RET:"+(int)(Double.parseDouble(data_version)-Double.parseDouble(disk_version))); |
| 191 | + if ((int) (Double.parseDouble(data_version) - Double.parseDouble(disk_version)) > 0 || disk_version.equals("0")) { |
| 192 | + try { |
| 193 | + FileOutputStream os = new FileOutputStream(disk_version_fn); |
| 194 | + try { |
| 195 | + os.write(data_version.getBytes()); |
| 196 | + os.close(); |
| 197 | + |
| 198 | + } catch (IOException e) { |
| 199 | + e.printStackTrace(); |
| 200 | + //Mint.logException(e); |
| 201 | + |
| 202 | + } |
| 203 | + |
| 204 | + } catch (FileNotFoundException e) { |
| 205 | + e.printStackTrace(); |
| 206 | + //Mint.logException(e); |
| 207 | + |
| 208 | + } |
| 209 | + |
| 210 | + |
| 211 | + return true; |
| 212 | + } else { |
| 213 | + return false; |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + public interface PermissionAction { |
| 218 | + void onGrant(); |
| 219 | + |
| 220 | + void onDeny(); |
| 221 | + } |
| 222 | + |
| 223 | + public void ifLogin(Login afterLogin) { |
| 224 | + if (!TextUtils.isEmpty(TokenManager.getToken())) { |
| 225 | + afterLogin.process(); |
| 226 | + } else { |
| 227 | + Toast.makeText(this, R.string.login_first, Toast.LENGTH_SHORT).show(); |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + public interface Login { |
| 232 | + void process(); |
| 233 | + } |
| 234 | + |
| 235 | +} |
0 commit comments