|
| 1 | +/* |
| 2 | + * Copyright 2018 Mr Duy |
| 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 | + |
| 17 | +package com.duy.ccppcompiler.console.activities; |
| 18 | + |
| 19 | +import android.content.ClipData; |
| 20 | +import android.content.ClipboardManager; |
| 21 | +import android.content.ComponentName; |
| 22 | +import android.content.Context; |
| 23 | +import android.content.Intent; |
| 24 | +import android.content.ServiceConnection; |
| 25 | +import android.net.Uri; |
| 26 | +import android.os.Bundle; |
| 27 | +import android.os.IBinder; |
| 28 | +import android.support.v7.app.AppCompatActivity; |
| 29 | +import android.support.v7.widget.Toolbar; |
| 30 | +import android.text.TextUtils; |
| 31 | +import android.util.TypedValue; |
| 32 | +import android.view.MenuItem; |
| 33 | + |
| 34 | +import com.duy.ccppcompiler.R; |
| 35 | +import com.duy.ccppcompiler.console.services.TermuxService; |
| 36 | +import com.duy.ccppcompiler.console.services.TermuxViewClient; |
| 37 | +import com.termux.terminal.TerminalSession; |
| 38 | +import com.termux.view.TerminalView; |
| 39 | + |
| 40 | +public class ConsoleActivity extends AppCompatActivity implements ServiceConnection { |
| 41 | + |
| 42 | + public static final String EXTRA_BINARY_FILE_PATH = "file_path"; |
| 43 | + private static final String TAG = "ConsoleActivity"; |
| 44 | + |
| 45 | + private static final int MAX_FONTSIZE = 256; |
| 46 | + private static int MIN_FONTSIZE; |
| 47 | + public TerminalView mEmulatorView; |
| 48 | + public TermuxService mTermService; |
| 49 | + private String cmd; |
| 50 | + private int mFontSize; |
| 51 | + |
| 52 | + @Override |
| 53 | + protected void onCreate(Bundle savedInstanceState) { |
| 54 | + super.onCreate(savedInstanceState); |
| 55 | + setContentView(R.layout.activity_console); |
| 56 | + setSupportActionBar((Toolbar) findViewById(R.id.toolbar)); |
| 57 | + getSupportActionBar().setDisplayHomeAsUpEnabled(true); |
| 58 | + |
| 59 | + computeFontSize(); |
| 60 | + initView(); |
| 61 | + startService(); |
| 62 | + } |
| 63 | + |
| 64 | + private void computeFontSize() { |
| 65 | + float dipInPixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, this.getResources().getDisplayMetrics()); |
| 66 | + MIN_FONTSIZE = (int) (4f * dipInPixels); |
| 67 | + int defaultFontSize = Math.round(12 * dipInPixels); |
| 68 | + // Make it divisible by 2 since that is the minimal adjustment step: |
| 69 | + if (defaultFontSize % 2 == 1) defaultFontSize--; |
| 70 | + |
| 71 | + mFontSize = defaultFontSize; |
| 72 | + mFontSize = Math.max(MIN_FONTSIZE, Math.min(mFontSize, MAX_FONTSIZE)); |
| 73 | + } |
| 74 | + |
| 75 | + private void initView() { |
| 76 | + mEmulatorView = findViewById(R.id.emulatorView); |
| 77 | + cmd = getIntent().getStringExtra(EXTRA_BINARY_FILE_PATH); |
| 78 | + mEmulatorView.setTextSize(mFontSize); |
| 79 | + mEmulatorView.requestFocus(); |
| 80 | + mEmulatorView.setOnKeyListener(new TermuxViewClient(this)); |
| 81 | + } |
| 82 | + |
| 83 | + private void startService() { |
| 84 | + Intent intent = new Intent(this, TermuxService.class); |
| 85 | + // Start the service and make it run regardless of who is bound to it: |
| 86 | + intent.setAction(TermuxService.ACTION_EXECUTE); |
| 87 | + String uriStr = "file:///" + cmd; |
| 88 | + intent.setData(Uri.parse(uriStr)); |
| 89 | + startService(intent); |
| 90 | + if (!bindService(intent, this, 0)) { |
| 91 | + throw new RuntimeException("bindService() failed"); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + protected void onStart() { |
| 97 | + super.onStart(); |
| 98 | + mEmulatorView.onScreenUpdated(); |
| 99 | + } |
| 100 | + |
| 101 | + public void doPaste() { |
| 102 | + ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); |
| 103 | + ClipData clipData = clipboard.getPrimaryClip(); |
| 104 | + if (clipData == null) return; |
| 105 | + CharSequence paste = clipData.getItemAt(0).coerceToText(this); |
| 106 | + if (!TextUtils.isEmpty(paste)) |
| 107 | + getCurrentTermSession().getEmulator().paste(paste.toString()); |
| 108 | + } |
| 109 | + |
| 110 | + private TerminalSession getCurrentTermSession() { |
| 111 | + return mTermService.getTermSession(); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + protected void onDestroy() { |
| 116 | + super.onDestroy(); |
| 117 | + unbindService(this); |
| 118 | + mTermService.stopSelf(); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 123 | + if (item.getItemId() == android.R.id.home) { |
| 124 | + finish(); |
| 125 | + } |
| 126 | + return super.onOptionsItemSelected(item); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void onServiceConnected(ComponentName componentName, IBinder service) { |
| 131 | + mTermService = ((TermuxService.LocalBinder) service).service; |
| 132 | + mEmulatorView.attachSession(mTermService.getTermSession()); |
| 133 | + mTermService.mSessionChangeCallback = new TerminalSession.SessionChangedCallback() { |
| 134 | + @Override |
| 135 | + public void onTextChanged(TerminalSession changedSession) { |
| 136 | + mEmulatorView.onScreenUpdated(); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public void onTitleChanged(TerminalSession updatedSession) { |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public void onSessionFinished(final TerminalSession finishedSession) { |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public void onClipboardText(TerminalSession session, String text) { |
| 150 | + ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); |
| 151 | + clipboard.setPrimaryClip(new ClipData(null, new String[]{"text/plain"}, new ClipData.Item(text))); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public void onBell(TerminalSession session) { |
| 156 | + |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public void onColorsChanged(TerminalSession changedSession) { |
| 162 | + |
| 163 | + } |
| 164 | + }; |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public void onServiceDisconnected(ComponentName componentName) { |
| 169 | + |
| 170 | + } |
| 171 | + |
| 172 | + public void changeFontSize(boolean increase) { |
| 173 | + mFontSize += (increase ? 1 : -1) * 2; |
| 174 | + mFontSize = Math.max(MIN_FONTSIZE, Math.min(mFontSize, MAX_FONTSIZE)); |
| 175 | + mEmulatorView.setTextSize(mFontSize); |
| 176 | + } |
| 177 | + |
| 178 | +} |
0 commit comments