com.pluscubed.logcat.helper.SaveLogHelper Java Examples

The following examples show how to use com.pluscubed.logcat.helper.SaveLogHelper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LogcatActivity.java    From matlog with GNU General Public License v3.0 6 votes vote down vote up
private void saveLog(final String filename) {

        // do in background to avoid jankiness

        final List<CharSequence> logLines = getCurrentLogAsListOfStrings();

        new Thread(() -> {
            SaveLogHelper.deleteLogIfExists(filename);
            final boolean saved = SaveLogHelper.saveLog(logLines, filename);

            mHandler.post(() -> {
                if (saved) {
                    Toast.makeText(getApplicationContext(), R.string.log_saved, Toast.LENGTH_SHORT).show();
                    openLogFile(filename);
                } else {
                    Toast.makeText(getApplicationContext(), R.string.unable_to_save_log, Toast.LENGTH_LONG).show();
                }
            });
        }).start();

    }
 
Example #2
Source File: LogcatActivity.java    From matlog with GNU General Public License v3.0 6 votes vote down vote up
private void showSaveLogDialog() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                SAVE_LOG_REQUEST);
        return;
    }

    if (!SaveLogHelper.checkSdCard(this)) {
        return;
    }

    MaterialDialog.InputCallback onClickListener = (materialDialog, charSequence) -> {
        if (DialogHelper.isInvalidFilename(charSequence)) {
            Toast.makeText(LogcatActivity.this, R.string.enter_good_filename, Toast.LENGTH_SHORT).show();
        } else {
            String filename = charSequence.toString();
            saveLog(filename);
        }
    };

    DialogHelper.showFilenameSuggestingDialog(this, null, onClickListener, R.string.save_log);
}
 
Example #3
Source File: LogFileAdapter.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
@Override
public View getView(int position, View view, @NonNull ViewGroup parent) {

    Context context = parent.getContext();

    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(resId, parent, false);
    }

    CheckBox box = (CheckBox) view.findViewById(android.R.id.checkbox);
    RadioButton button = (RadioButton) view.findViewById(android.R.id.button1);
    TextView text1 = (TextView) view.findViewById(android.R.id.text1);
    TextView text2 = (TextView) view.findViewById(android.R.id.text2);

    CharSequence filename = objects.get(position);

    text1.setText(filename);


    if (multiMode) {
        box.setChecked(checkedItems[position]);
    } else {
        button.setChecked(checked == position);
    }

    Date lastModified = SaveLogHelper.getLastModifiedDate(filename.toString());
    DateFormat dateFormat = DateFormat.getDateTimeInstance();

    text2.setText(dateFormat.format(lastModified));

    return view;
}
 
Example #4
Source File: LogFileAdapter.java    From matlog with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
@Override
public View getView(int position, View view, @NonNull ViewGroup parent) {

    Context context = parent.getContext();

    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(resId, parent, false);
    }

    CheckBox box = view.findViewById(android.R.id.checkbox);
    RadioButton button = view.findViewById(android.R.id.button1);
    TextView text1 = view.findViewById(android.R.id.text1);
    TextView text2 = view.findViewById(android.R.id.text2);

    CharSequence filename = objects.get(position);

    text1.setText(filename);


    if (multiMode) {
        box.setChecked(checkedItems[position]);
    } else {
        button.setChecked(checked == position);
    }

    Date lastModified = SaveLogHelper.getLastModifiedDate(filename.toString());
    DateFormat dateFormat = DateFormat.getDateTimeInstance();

    text2.setText(dateFormat.format(lastModified));

    return view;
}
 
Example #5
Source File: LogcatActivity.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private void saveLog(final String filename) {

        // do in background to avoid jankiness

        final List<CharSequence> logLines = getCurrentLogAsListOfStrings();

        new Thread(new Runnable() {
            @Override
            public void run() {
                SaveLogHelper.deleteLogIfExists(filename);
                final boolean saved = SaveLogHelper.saveLog(logLines, filename);

                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (saved) {
                            Toast.makeText(getApplicationContext(), R.string.log_saved, Toast.LENGTH_SHORT).show();
                            openLogFile(filename);
                        } else {
                            Toast.makeText(getApplicationContext(), R.string.unable_to_save_log, Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }
        }).start();

    }
 
Example #6
Source File: LogFileAdapter.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
public View getView(int position, View view, @NonNull ViewGroup parent) {

    Context context = parent.getContext();

    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(resId, parent, false);
    }

    CheckBox box = (CheckBox) view.findViewById(android.R.id.checkbox);
    RadioButton button = (RadioButton) view.findViewById(android.R.id.button1);
    TextView text1 = (TextView) view.findViewById(android.R.id.text1);
    TextView text2 = (TextView) view.findViewById(android.R.id.text2);

    CharSequence filename = objects.get(position);

    text1.setText(filename);


    if (multiMode) {
        box.setChecked(checkedItems[position]);
    } else {
        button.setChecked(checked == position);
    }

    Date lastModified = SaveLogHelper.getLastModifiedDate(filename.toString());
    DateFormat dateFormat = DateFormat.getDateTimeInstance();

    text2.setText(dateFormat.format(lastModified));

    return view;
}
 
Example #7
Source File: LogcatActivity.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private void showSaveLogDialog() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                SAVE_LOG_REQUEST);
        return;
    }

    if (!SaveLogHelper.checkSdCard(this)) {
        return;
    }

    MaterialDialog.InputCallback onClickListener = new MaterialDialog.InputCallback() {
        @Override
        public void onInput(@NonNull MaterialDialog materialDialog, CharSequence charSequence) {
            if (DialogHelper.isInvalidFilename(charSequence)) {
                Toast.makeText(LogcatActivity.this, R.string.enter_good_filename, Toast.LENGTH_SHORT).show();
            } else {
                String filename = charSequence.toString();
                saveLog(filename);
            }
        }
    };

    DialogHelper.showFilenameSuggestingDialog(this, null, onClickListener, R.string.save_log);
}
 
Example #8
Source File: LogcatActivity.java    From matlog with GNU General Public License v3.0 5 votes vote down vote up
protected void sendLogToTargetApp(final boolean asText, final boolean includeDeviceInfo, final boolean includeDmesg) {

        if (!(mCurrentlyOpenLog == null && asText) && !SaveLogHelper.checkSdCard(this)) {
            // if asText is false, then we need to check to make sure we can access the sdcard
            return;
        }

        final Handler ui = new Handler(Looper.getMainLooper());
        new Thread(new Runnable() {
            private MaterialDialog mDialog;

            @Override
            public void run() {
                ui.post(() -> {
                    if (asText || mCurrentlyOpenLog == null || includeDeviceInfo || includeDmesg) {
                        MaterialDialog.Builder progressDialog = new MaterialDialog.Builder(LogcatActivity.this);
                        progressDialog.title(R.string.dialog_please_wait);
                        progressDialog.content(getString(R.string.dialog_compiling_log));
                        progressDialog.progress(true, 0);
                        mDialog = progressDialog.show();
                        mDialog.setCanceledOnTouchOutside(false);
                        mDialog.setCancelable(false);
                    }
                });
                final SendLogDetails sendLogDetails = getSendLogDetails(asText, includeDeviceInfo, includeDmesg);
                ui.post(() -> {
                    startChooser(LogcatActivity.this, sendLogDetails.getSubject(), sendLogDetails.getBody(),
                            sendLogDetails.getAttachmentType(), sendLogDetails.getAttachment());
                    if (mDialog != null && mDialog.isShowing()) {
                        mDialog.dismiss();
                    }
                    if (asText && sendLogDetails.getBody().length() > 100000) {
                        Snackbar.make(findViewById(android.R.id.content), getString(R.string.as_text_not_work), Snackbar.LENGTH_LONG).show();
                    }
                });
            }
        }).start();

    }
 
Example #9
Source File: LogcatActivity.java    From matlog with GNU General Public License v3.0 5 votes vote down vote up
protected void saveLogToTargetApp(final boolean includeDeviceInfo, final boolean includeDmesg) {

        if (!SaveLogHelper.checkSdCard(this)) {
            // if asText is false, then we need to check to make sure we can access the sdcard
            return;
        }

        final Handler ui = new Handler(Looper.getMainLooper());
        new Thread(new Runnable() {
            private MaterialDialog mDialog;

            @Override
            public void run() {
                ui.post(() -> {
                    if (mCurrentlyOpenLog == null || includeDeviceInfo || includeDmesg) {
                        MaterialDialog.Builder progressDialog = new MaterialDialog.Builder(LogcatActivity.this);
                        progressDialog.title(R.string.dialog_please_wait);
                        progressDialog.content(getString(R.string.dialog_compiling_log));
                        progressDialog.progress(true, 0);
                        mDialog = progressDialog.show();
                        mDialog.setCanceledOnTouchOutside(false);
                        mDialog.setCancelable(false);
                    }
                });
                final File zipFile = saveLogAsZip(includeDeviceInfo, includeDmesg);
                ui.post(() -> {
                    if (mDialog != null && mDialog.isShowing()) {
                        mDialog.dismiss();
                    }
                    Toast.makeText(getApplicationContext(), R.string.log_saved, Toast.LENGTH_SHORT).show();
                });
            }
        }).start();

    }
 
Example #10
Source File: LogcatActivity.java    From matlog with GNU General Public License v3.0 5 votes vote down vote up
private File saveLogAsZip(boolean includeDeviceInfo, boolean includeDmesg) {
    List<File> files = new ArrayList<>();
    SaveLogHelper.cleanTemp();

    if (mCurrentlyOpenLog != null) { // use saved log file
        files.add(SaveLogHelper.getFile(mCurrentlyOpenLog));
    } else { // create a temp file to hold the current, unsaved log
        File tempLogFile = SaveLogHelper.saveTemporaryFile(this,
                SaveLogHelper.TEMP_LOG_FILENAME, null, getCurrentLogAsListOfStrings());
        files.add(tempLogFile);
    }

    if (includeDeviceInfo) {
        // include device info
        String deviceInfo = BuildHelper.getBuildInformationAsString();
        // or create as separate file called device.txt
        File tempFile = SaveLogHelper.saveTemporaryFile(this,
                SaveLogHelper.TEMP_DEVICE_INFO_FILENAME, deviceInfo, null);
        files.add(tempFile);
    }

    if (includeDmesg) {
        File tempDmsgFile = SaveLogHelper.saveTemporaryFile(this,
                SaveLogHelper.TEMP_DMESG_FILENAME, null, DmesgHelper.getDmsg());
        files.add(tempDmsgFile);
    }

    File zipFile = SaveLogHelper.saveZipFile(SaveLogHelper.createLogFilename(true), files);

    return zipFile;
}
 
Example #11
Source File: LogcatActivity.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
private void saveLog(final String filename) {

        // do in background to avoid jankiness

        final List<CharSequence> logLines = getCurrentLogAsListOfStrings();

        new Thread(new Runnable() {
            @Override
            public void run() {
                SaveLogHelper.deleteLogIfExists(filename);
                final boolean saved = SaveLogHelper.saveLog(logLines, filename);

                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (saved) {
                            Toast.makeText(getApplicationContext(), R.string.log_saved, Toast.LENGTH_SHORT).show();
                            openLogFile(filename);
                        } else {
                            Toast.makeText(getApplicationContext(), R.string.unable_to_save_log, Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }
        }).start();

    }
 
Example #12
Source File: LogcatActivity.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
private void showSaveLogDialog() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                SAVE_LOG_REQUEST);
        return;
    }

    if (!SaveLogHelper.checkSdCard(this)) {
        return;
    }

    MaterialDialog.InputCallback onClickListener = new MaterialDialog.InputCallback() {
        @Override
        public void onInput(@NonNull MaterialDialog materialDialog, CharSequence charSequence) {
            if (DialogHelper.isInvalidFilename(charSequence)) {
                Toast.makeText(LogcatActivity.this, R.string.enter_good_filename, Toast.LENGTH_SHORT).show();
            } else {
                String filename = charSequence.toString();
                saveLog(filename);
            }
        }
    };

    DialogHelper.showFilenameSuggestingDialog(this, null, onClickListener, R.string.save_log);
}
 
Example #13
Source File: LogcatActivity.java    From matlog with GNU General Public License v3.0 5 votes vote down vote up
private void completePartialSelect() {

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    COMPLETE_PARTIAL_SELECT_REQUEST);
            return;
        }
        if (!SaveLogHelper.checkSdCard(this)) {
            cancelPartialSelect();
            return;
        }

        MaterialDialog.InputCallback onClickListener = (materialDialog, charSequence) -> {
            if (DialogHelper.isInvalidFilename(charSequence)) {
                cancelPartialSelect();
                Toast.makeText(LogcatActivity.this, R.string.enter_good_filename, Toast.LENGTH_SHORT).show();
            } else {
                String filename = charSequence.toString();
                if (partiallySelectedLogLines.size() == 2)
                    savePartialLog(filename, partiallySelectedLogLines.get(0), partiallySelectedLogLines.get(1));
            }
        };


        MaterialDialog.SingleButtonCallback onCancelListener = (dialog, which) -> {
            if(which == DialogAction.NEGATIVE) {
                cancelPartialSelect();
            }
        };

        DialogHelper.showFilenameSuggestingDialog(this, onCancelListener, onClickListener, R.string.save_log);

    }
 
Example #14
Source File: LogcatActivity.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
private void savePartialLog(final String filename, LogLine first, LogLine last) {

        final List<CharSequence> logLines = new ArrayList<>(mLogListAdapter.getItemCount());

        // filter based on first and last
        boolean started = false;
        boolean foundLast = false;
        for (int i = 0; i < mLogListAdapter.getItemCount(); i++) {
            LogLine logLine = mLogListAdapter.getItem(i);
            if (logLine == first) {
                started = true;
            }
            if (started) {
                logLines.add(logLine.getOriginalLine());
            }
            if (logLine == last) {
                foundLast = true;
                break;
            }
        }

        if (!foundLast || logLines.isEmpty()) {
            Toast.makeText(this, R.string.toast_invalid_selection, Toast.LENGTH_LONG).show();
            cancelPartialSelect();
            return;
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                SaveLogHelper.deleteLogIfExists(filename);
                final boolean saved = SaveLogHelper.saveLog(logLines, filename);

                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (saved) {
                            Toast.makeText(getApplicationContext(), R.string.log_saved, Toast.LENGTH_SHORT).show();
                            openLogFile(filename);
                        } else {
                            Toast.makeText(getApplicationContext(), R.string.unable_to_save_log, Toast.LENGTH_LONG).show();
                        }
                        cancelPartialSelect();
                    }
                });
            }
        }).start();
    }
 
Example #15
Source File: LogcatActivity.java    From matlog with GNU General Public License v3.0 4 votes vote down vote up
@WorkerThread
private SendLogDetails getSendLogDetails(boolean asText, boolean includeDeviceInfo, boolean includeDmesg) {
    SendLogDetails sendLogDetails = new SendLogDetails();
    StringBuilder body = new StringBuilder();

    List<File> files = new ArrayList<>();
    SaveLogHelper.cleanTemp();

    if (!asText) {
        if (mCurrentlyOpenLog != null) { // use saved log file
            files.add(SaveLogHelper.getFile(mCurrentlyOpenLog));
        } else { // create a temp file to hold the current, unsaved log
            File tempLogFile = SaveLogHelper.saveTemporaryFile(this,
                    SaveLogHelper.TEMP_LOG_FILENAME, null, getCurrentLogAsListOfStrings());
            files.add(tempLogFile);
        }
    }

    if (includeDeviceInfo) {
        // include device info
        String deviceInfo = BuildHelper.getBuildInformationAsString();
        if (asText) {
            // append to top of body
            body.append(deviceInfo).append('\n');
        } else {
            // or create as separate file called device.txt
            File tempFile = SaveLogHelper.saveTemporaryFile(this,
                    SaveLogHelper.TEMP_DEVICE_INFO_FILENAME, deviceInfo, null);
            files.add(tempFile);
        }
    }

    if (includeDmesg) {
        File tempDmsgFile = SaveLogHelper.saveTemporaryFile(this,
                SaveLogHelper.TEMP_DMESG_FILENAME, null, DmesgHelper.getDmsg());
        files.add(tempDmsgFile);
    }

    if (asText) {
        body.append(getCurrentLogAsCharSequence());
    }

    sendLogDetails.setBody(body.toString());
    sendLogDetails.setSubject(getString(R.string.subject_log_report));

    // either zip up multiple files or just attach the one file
    switch (files.size()) {
        case 0: // no attachments
            sendLogDetails.setAttachmentType(SendLogDetails.AttachmentType.None);
            break;
        case 1: // one plaintext file attachment
            sendLogDetails.setAttachmentType(SendLogDetails.AttachmentType.Text);
            sendLogDetails.setAttachment(files.get(0));
            break;
        default: // 2 files - need to zip them up
            File zipFile = SaveLogHelper.saveTemporaryZipFile(SaveLogHelper.createLogFilename(true), files);

            sendLogDetails.setSubject(zipFile.getName());
            sendLogDetails.setAttachmentType(SendLogDetails.AttachmentType.Zip);
            sendLogDetails.setAttachment(zipFile);
            break;
    }

    return sendLogDetails;
}
 
Example #16
Source File: LogcatActivity.java    From matlog with GNU General Public License v3.0 4 votes vote down vote up
protected void verifyDelete(final CharSequence[] filenameArray,
                            final boolean[] checkedItems, final DialogInterface parentDialog) {

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    int deleteCount = 0;

    for (boolean checkedItem : checkedItems) {
        if (checkedItem) {
            deleteCount++;
        }
    }


    final int finalDeleteCount = deleteCount;

    if (finalDeleteCount > 0) {

        builder.setTitle(R.string.delete_saved_log)
                .setCancelable(true)
                .setMessage(getResources().getQuantityString(R.plurals.are_you_sure, finalDeleteCount, finalDeleteCount))
                .setPositiveButton(android.R.string.ok, (dialog, which) -> {
                    // ok, delete

                    for (int i = 0; i < checkedItems.length; i++) {
                        if (checkedItems[i]) {
                            SaveLogHelper.deleteLogIfExists(filenameArray[i].toString());
                        }
                    }

                    String toastText = getResources().getQuantityString(R.plurals.files_deleted, finalDeleteCount, finalDeleteCount);
                    Toast.makeText(LogcatActivity.this, toastText, Toast.LENGTH_SHORT).show();

                    dialog.dismiss();
                    parentDialog.dismiss();

                });
        builder.setNegativeButton(android.R.string.cancel, null);
        builder.show();
    }


}
 
Example #17
Source File: LogcatActivity.java    From matlog with GNU General Public License v3.0 4 votes vote down vote up
private void savePartialLog(final String filename, LogLine first, LogLine last) {

        final List<CharSequence> logLines = new ArrayList<>(mLogListAdapter.getItemCount());

        // filter based on first and last
        boolean started = false;
        boolean foundLast = false;
        for (int i = 0; i < mLogListAdapter.getItemCount(); i++) {
            LogLine logLine = mLogListAdapter.getItem(i);
            if (logLine == first) {
                started = true;
            }
            if (started) {
                logLines.add(logLine.getOriginalLine());
            }
            if (logLine == last) {
                foundLast = true;
                break;
            }
        }

        if (!foundLast || logLines.isEmpty()) {
            Toast.makeText(this, R.string.toast_invalid_selection, Toast.LENGTH_LONG).show();
            cancelPartialSelect();
            return;
        }

        new Thread(() -> {
            SaveLogHelper.deleteLogIfExists(filename);
            final boolean saved = SaveLogHelper.saveLog(logLines, filename);

            mHandler.post(() -> {
                if (saved) {
                    Toast.makeText(getApplicationContext(), R.string.log_saved, Toast.LENGTH_SHORT).show();
                    openLogFile(filename);
                } else {
                    Toast.makeText(getApplicationContext(), R.string.unable_to_save_log, Toast.LENGTH_LONG).show();
                }
                cancelPartialSelect();
            });
        }).start();
    }
 
Example #18
Source File: LogcatActivity.java    From matlog with GNU General Public License v3.0 4 votes vote down vote up
private void showOpenLogFileDialog() {

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    OPEN_LOG_REQUEST);
            return;
        }
        if (!SaveLogHelper.checkSdCard(this)) {
            return;
        }

        final List<CharSequence> filenames = new ArrayList<>(SaveLogHelper.getLogFilenames());

        if (filenames.isEmpty()) {
            Toast.makeText(this, R.string.no_saved_logs, Toast.LENGTH_SHORT).show();
            return;
        }

        int logToSelect = mCurrentlyOpenLog != null ? filenames.indexOf(mCurrentlyOpenLog) : -1;
        ArrayAdapter<CharSequence> logFileAdapter = new LogFileAdapter(this, filenames, logToSelect, false);

        ListView view = new ListView(this);
        view.setAdapter(logFileAdapter);
        view.setDivider(null);
        view.setDividerHeight(0);

        MaterialDialog.Builder builder = new MaterialDialog.Builder(this);
        builder.title(R.string.open_log)
                .customView(view, false);

        final MaterialDialog dialog = builder.show();


        view.setOnItemClickListener((parent, view1, position, id) -> {
            dialog.dismiss();
            String filename = filenames.get(position).toString();
            openLogFile(filename);
        });

    }
 
Example #19
Source File: LogcatActivity.java    From matlog with GNU General Public License v3.0 4 votes vote down vote up
private void startDeleteSavedLogsDialog() {

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    DELETE_SAVED_LOG_REQUEST);
            return;
        }
        if (!SaveLogHelper.checkSdCard(this)) {
            return;
        }

        List<CharSequence> filenames = new ArrayList<>(SaveLogHelper.getLogFilenames());

        if (filenames.isEmpty()) {
            Toast.makeText(this, R.string.no_saved_logs, Toast.LENGTH_SHORT).show();
            return;
        }

        final CharSequence[] filenameArray = ArrayUtil.toArray(filenames, CharSequence.class);

        final LogFileAdapter logFileAdapter = new LogFileAdapter(this, filenames, -1, true);

        @SuppressLint("InflateParams") LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.dialog_delete_logfiles, null);

        ListView view = layout.findViewById(R.id.list);
        view.setAdapter(logFileAdapter);

        MaterialDialog.Builder builder = new MaterialDialog.Builder(this);
        builder.title(R.string.manage_saved_logs)
                .customView(layout, false)
                .negativeText(android.R.string.cancel)
                .neutralText(R.string.delete_all)
                .onNeutral((dialog, which) -> {
                    boolean[] allChecked = new boolean[logFileAdapter.getCount()];

                    for (int i = 0; i < allChecked.length; i++) {
                        allChecked[i] = true;
                    }
                    verifyDelete(filenameArray, allChecked, dialog);
                })
                .onPositive((dialog, which) -> verifyDelete(filenameArray, logFileAdapter.getCheckedItems(), dialog))
                .positiveText(R.string.delete);

        builder.show();

        view.setOnItemClickListener((parent, view1, position, id) -> logFileAdapter.checkOrUncheck(position));
    }
 
Example #20
Source File: LogcatRecordingService.java    From matlog with GNU General Public License v3.0 4 votes vote down vote up
private void handleIntent(Intent intent) {

        log.d("Starting up %s now with intent: %s", LogcatRecordingService.class.getSimpleName(), intent);

        String filename = intent.getStringExtra(EXTRA_FILENAME);
        String queryText = intent.getStringExtra(EXTRA_QUERY_FILTER);
        String logLevel = intent.getStringExtra(EXTRA_LEVEL);

        SearchCriteria searchCriteria = new SearchCriteria(queryText);

        CharSequence[] logLevels = getResources().getStringArray(R.array.log_levels_values);
        int logLevelLimit = ArrayUtil.indexOf(logLevels, logLevel);

        boolean searchCriteriaWillAlwaysMatch = searchCriteria.isEmpty();
        boolean logLevelAcceptsEverything = logLevelLimit == 0;

        SaveLogHelper.deleteLogIfExists(filename);

        initializeReader(intent);

        StringBuilder stringBuilder = new StringBuilder();

        try {

            String line;
            int lineCount = 0;
            int logLinePeriod = PreferenceHelper.getLogLinePeriodPreference(this);
            String filterPattern = PreferenceHelper.getFilterPatternPreference(this);
            while (mReader != null && (line = mReader.readLine()) != null && !mKilled) {

                // filter
                if (!searchCriteriaWillAlwaysMatch || !logLevelAcceptsEverything) {
                    if (!checkLogLine(line, searchCriteria, logLevelLimit, filterPattern)) {
                        continue;
                    }
                }

                stringBuilder.append(line).append("\n");

                if (++lineCount % logLinePeriod == 0) {
                    // avoid OutOfMemoryErrors; flush now
                    SaveLogHelper.saveLog(stringBuilder, filename);
                    stringBuilder.delete(0, stringBuilder.length()); // clear
                }
            }
        } catch (IOException e) {
            log.e(e, "unexpected exception");
        } finally {
            killProcess();
            log.d("CatlogService ended");

            boolean logSaved = SaveLogHelper.saveLog(stringBuilder, filename);

            if (logSaved) {
                makeToast(R.string.log_saved, Toast.LENGTH_SHORT);
                startLogcatActivityToViewSavedFile(filename);
            } else {
                makeToast(R.string.unable_to_save_log, Toast.LENGTH_LONG);
            }
        }
    }
 
Example #21
Source File: LogcatActivity.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
private void completePartialSelect() {

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    COMPLETE_PARTIAL_SELECT_REQUEST);
            return;
        }
        if (!SaveLogHelper.checkSdCard(this)) {
            cancelPartialSelect();
            return;
        }

        MaterialDialog.InputCallback onClickListener = new MaterialDialog.InputCallback() {

            @Override
            public void onInput(@NonNull MaterialDialog materialDialog, CharSequence charSequence) {
                if (DialogHelper.isInvalidFilename(charSequence)) {
                    cancelPartialSelect();
                    Toast.makeText(LogcatActivity.this, R.string.enter_good_filename, Toast.LENGTH_SHORT).show();
                } else {
                    String filename = charSequence.toString();
                    savePartialLog(filename, partiallySelectedLogLines.get(0), partiallySelectedLogLines.get(1));
                }
            }
        };


        MaterialDialog.SingleButtonCallback onCancelListener = new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                if (which == DialogAction.NEGATIVE) {
                    cancelPartialSelect();
                }
            }
        };

        DialogHelper.showFilenameSuggestingDialog(this, onCancelListener, onClickListener, R.string.save_log);

    }
 
Example #22
Source File: LogcatActivity.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@WorkerThread
private SendLogDetails getSendLogDetails(boolean asText, boolean includeDeviceInfo) {
    SendLogDetails sendLogDetails = new SendLogDetails();
    StringBuilder body = new StringBuilder();

    List<File> files = new ArrayList<>();

    if (!asText) {
        if (mCurrentlyOpenLog != null) { // use saved log file
            files.add(SaveLogHelper.getFile(mCurrentlyOpenLog));
        } else { // create a temp file to hold the current, unsaved log
            File tempLogFile = SaveLogHelper.saveTemporaryFile(this,
                    SaveLogHelper.TEMP_LOG_FILENAME, null, getCurrentLogAsListOfStrings());
            files.add(tempLogFile);
        }
    }

    if (includeDeviceInfo) {
        // include device info
        String deviceInfo = BuildHelper.getBuildInformationAsString();
        if (asText) {
            // append to top of body
            body.append(deviceInfo).append('\n');
        } else {
            // or create as separate file called device.txt
            File tempFile = SaveLogHelper.saveTemporaryFile(this,
                    SaveLogHelper.TEMP_DEVICE_INFO_FILENAME, deviceInfo, null);
            files.add(tempFile);
        }
    }

    if (asText) {
        body.append(getCurrentLogAsCharSequence());
    }

    sendLogDetails.setBody(body.toString());
    sendLogDetails.setSubject(getString(R.string.subject_log_report));

    // either zip up multiple files or just attach the one file
    switch (files.size()) {
        case 0: // no attachments
            sendLogDetails.setAttachmentType(SendLogDetails.AttachmentType.None);
            break;
        case 1: // one plaintext file attachment
            sendLogDetails.setAttachmentType(SendLogDetails.AttachmentType.Text);
            sendLogDetails.setAttachment(files.get(0));
            break;
        default: // 2 files - need to zip them up
            File zipFile = SaveLogHelper.saveTemporaryZipFile(SaveLogHelper.TEMP_ZIP_FILENAME, files);
            File tmpDirectory = SaveLogHelper.getTempDirectory();
            for (File file : files) {
                // delete original files
                if (file.getParentFile().equals(tmpDirectory)) { // only delete temporary files
                    //noinspection ResultOfMethodCallIgnored
                    file.delete();
                }
            }
            sendLogDetails.setAttachmentType(SendLogDetails.AttachmentType.Zip);
            sendLogDetails.setAttachment(zipFile);
            break;
    }

    return sendLogDetails;
}
 
Example #23
Source File: LogcatActivity.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
protected void sendLogToTargetApp(final boolean asText, final boolean includeDeviceInfo) {

        if (!(mCurrentlyOpenLog == null && asText) && !SaveLogHelper.checkSdCard(this)) {
            // if asText is false, then we need to check to make sure we can access the sdcard
            return;
        }

        final Handler ui = new Handler(Looper.getMainLooper());
        new Thread(new Runnable() {
            private MaterialDialog mDialog;

            @Override
            public void run() {
                ui.post(new Runnable() {
                    @Override
                    public void run() {
                        if (asText || mCurrentlyOpenLog == null || includeDeviceInfo) {
                            MaterialDialog.Builder progressDialog = new MaterialDialog.Builder(LogcatActivity.this);
                            progressDialog.title(R.string.dialog_please_wait);
                            progressDialog.content(getString(R.string.dialog_compiling_log));
                            progressDialog.progress(true, 0);
                            mDialog = progressDialog.show();
                            mDialog.setCanceledOnTouchOutside(false);
                            mDialog.setCancelable(false);
                        }
                    }
                });
                final SendLogDetails sendLogDetails = getSendLogDetails(asText, includeDeviceInfo);
                ui.post(new Runnable() {
                    @Override
                    public void run() {
                        startChooser(LogcatActivity.this, sendLogDetails.getSubject(), sendLogDetails.getBody(),
                                sendLogDetails.getAttachmentType(), sendLogDetails.getAttachment());
                        if (mDialog != null && mDialog.isShowing()) {
                            mDialog.dismiss();
                        }
                        if (asText && sendLogDetails.getBody().length() > 100000) {
                            Snackbar.make(findViewById(android.R.id.content), getString(R.string.as_text_not_work), Snackbar.LENGTH_LONG).show();
                        }
                    }
                });
            }
        }).start();

    }
 
Example #24
Source File: LogcatActivity.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
protected void verifyDelete(final CharSequence[] filenameArray,
                            final boolean[] checkedItems, final DialogInterface parentDialog) {

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    int deleteCount = 0;

    for (boolean checkedItem : checkedItems) {
        if (checkedItem) {
            deleteCount++;
        }
    }


    final int finalDeleteCount = deleteCount;

    if (finalDeleteCount > 0) {

        builder.setTitle(R.string.delete_saved_log)
                .setCancelable(true)
                .setMessage(getResources().getQuantityString(R.plurals.are_you_sure, finalDeleteCount))
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // ok, delete

                        for (int i = 0; i < checkedItems.length; i++) {
                            if (checkedItems[i]) {
                                SaveLogHelper.deleteLogIfExists(filenameArray[i].toString());
                            }
                        }

                        String toastText = getResources().getQuantityString(R.plurals.files_deleted, finalDeleteCount);
                        Toast.makeText(LogcatActivity.this, toastText, Toast.LENGTH_SHORT).show();

                        dialog.dismiss();
                        parentDialog.dismiss();

                    }
                });
        builder.setNegativeButton(android.R.string.cancel, null);
        builder.show();
    }


}
 
Example #25
Source File: LogcatRecordingService.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
private void handleIntent(Intent intent) {

        log.d("Starting up %s now with intent: %s", LogcatRecordingService.class.getSimpleName(), intent);

        String filename = intent.getStringExtra(EXTRA_FILENAME);
        String queryText = intent.getStringExtra(EXTRA_QUERY_FILTER);
        String logLevel = intent.getStringExtra(EXTRA_LEVEL);

        SearchCriteria searchCriteria = new SearchCriteria(queryText);

        CharSequence[] logLevels = getResources().getStringArray(R.array.log_levels_values);
        int logLevelLimit = ArrayUtil.indexOf(logLevels, logLevel);

        boolean searchCriteriaWillAlwaysMatch = searchCriteria.isEmpty();
        boolean logLevelAcceptsEverything = logLevelLimit == 0;

        SaveLogHelper.deleteLogIfExists(filename);

        initializeReader(intent);

        StringBuilder stringBuilder = new StringBuilder();

        try {

            String line;
            int lineCount = 0;
            int logLinePeriod = PreferenceHelper.getLogLinePeriodPreference(getApplicationContext());
            while ((line = mReader.readLine()) != null && !mKilled) {

                // filter
                if (!searchCriteriaWillAlwaysMatch || !logLevelAcceptsEverything) {
                    if (!checkLogLine(line, searchCriteria, logLevelLimit)) {
                        continue;
                    }
                }

                stringBuilder.append(line).append("\n");

                if (++lineCount % logLinePeriod == 0) {
                    // avoid OutOfMemoryErrors; flush now
                    SaveLogHelper.saveLog(stringBuilder, filename);
                    stringBuilder.delete(0, stringBuilder.length()); // clear
                }
            }
        } catch (IOException e) {
            log.e(e, "unexpected exception");
        } finally {
            killProcess();
            log.d("CatlogService ended");

            boolean logSaved = SaveLogHelper.saveLog(stringBuilder, filename);

            if (logSaved) {
                makeToast(R.string.log_saved, Toast.LENGTH_SHORT);
                startLogcatActivityToViewSavedFile(filename);
            } else {
                makeToast(R.string.unable_to_save_log, Toast.LENGTH_LONG);
            }
        }
    }
 
Example #26
Source File: LogcatActivity.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
private void completePartialSelect() {

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    COMPLETE_PARTIAL_SELECT_REQUEST);
            return;
        }
        if (!SaveLogHelper.checkSdCard(this)) {
            cancelPartialSelect();
            return;
        }

        MaterialDialog.InputCallback onClickListener = new MaterialDialog.InputCallback() {

            @Override
            public void onInput(@NonNull MaterialDialog materialDialog, CharSequence charSequence) {
                if (DialogHelper.isInvalidFilename(charSequence)) {
                    cancelPartialSelect();
                    Toast.makeText(LogcatActivity.this, R.string.enter_good_filename, Toast.LENGTH_SHORT).show();
                } else {
                    String filename = charSequence.toString();
                    savePartialLog(filename, partiallySelectedLogLines.get(0), partiallySelectedLogLines.get(1));
                }
            }
        };


        MaterialDialog.SingleButtonCallback onCancelListener = new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                if (which == DialogAction.NEGATIVE) {
                    cancelPartialSelect();
                }
            }
        };

        DialogHelper.showFilenameSuggestingDialog(this, onCancelListener, onClickListener, R.string.save_log);

    }
 
Example #27
Source File: LogcatActivity.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
private void savePartialLog(final String filename, LogLine first, LogLine last) {

        final List<CharSequence> logLines = new ArrayList<>(mLogListAdapter.getItemCount());

        // filter based on first and last
        boolean started = false;
        boolean foundLast = false;
        for (int i = 0; i < mLogListAdapter.getItemCount(); i++) {
            LogLine logLine = mLogListAdapter.getItem(i);
            if (logLine == first) {
                started = true;
            }
            if (started) {
                logLines.add(logLine.getOriginalLine());
            }
            if (logLine == last) {
                foundLast = true;
                break;
            }
        }

        if (!foundLast || logLines.isEmpty()) {
            Toast.makeText(this, R.string.toast_invalid_selection, Toast.LENGTH_LONG).show();
            cancelPartialSelect();
            return;
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                SaveLogHelper.deleteLogIfExists(filename);
                final boolean saved = SaveLogHelper.saveLog(logLines, filename);

                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (saved) {
                            Toast.makeText(getApplicationContext(), R.string.log_saved, Toast.LENGTH_SHORT).show();
                            openLogFile(filename);
                        } else {
                            Toast.makeText(getApplicationContext(), R.string.unable_to_save_log, Toast.LENGTH_LONG).show();
                        }
                        cancelPartialSelect();
                    }
                });
            }
        }).start();
    }
 
Example #28
Source File: LogcatActivity.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
@WorkerThread
private SendLogDetails getSendLogDetails(boolean asText, boolean includeDeviceInfo) {
    SendLogDetails sendLogDetails = new SendLogDetails();
    StringBuilder body = new StringBuilder();

    List<File> files = new ArrayList<>();

    if (!asText) {
        if (mCurrentlyOpenLog != null) { // use saved log file
            files.add(SaveLogHelper.getFile(mCurrentlyOpenLog));
        } else { // create a temp file to hold the current, unsaved log
            File tempLogFile = SaveLogHelper.saveTemporaryFile(this,
                    SaveLogHelper.TEMP_LOG_FILENAME, null, getCurrentLogAsListOfStrings());
            files.add(tempLogFile);
        }
    }

    if (includeDeviceInfo) {
        // include device info
        String deviceInfo = BuildHelper.getBuildInformationAsString();
        if (asText) {
            // append to top of body
            body.append(deviceInfo).append('\n');
        } else {
            // or create as separate file called device.txt
            File tempFile = SaveLogHelper.saveTemporaryFile(this,
                    SaveLogHelper.TEMP_DEVICE_INFO_FILENAME, deviceInfo, null);
            files.add(tempFile);
        }
    }

    if (asText) {
        body.append(getCurrentLogAsCharSequence());
    }

    sendLogDetails.setBody(body.toString());
    sendLogDetails.setSubject(getString(R.string.subject_log_report));

    // either zip up multiple files or just attach the one file
    switch (files.size()) {
        case 0: // no attachments
            sendLogDetails.setAttachmentType(SendLogDetails.AttachmentType.None);
            break;
        case 1: // one plaintext file attachment
            sendLogDetails.setAttachmentType(SendLogDetails.AttachmentType.Text);
            sendLogDetails.setAttachment(files.get(0));
            break;
        default: // 2 files - need to zip them up
            File zipFile = SaveLogHelper.saveTemporaryZipFile(SaveLogHelper.TEMP_ZIP_FILENAME, files);
            File tmpDirectory = SaveLogHelper.getTempDirectory();
            for (File file : files) {
                // delete original files
                if (file.getParentFile().equals(tmpDirectory)) { // only delete temporary files
                    //noinspection ResultOfMethodCallIgnored
                    file.delete();
                }
            }
            sendLogDetails.setAttachmentType(SendLogDetails.AttachmentType.Zip);
            sendLogDetails.setAttachment(zipFile);
            break;
    }

    return sendLogDetails;
}
 
Example #29
Source File: LogcatActivity.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
protected void sendLogToTargetApp(final boolean asText, final boolean includeDeviceInfo) {

        if (!(mCurrentlyOpenLog == null && asText) && !SaveLogHelper.checkSdCard(this)) {
            // if asText is false, then we need to check to make sure we can access the sdcard
            return;
        }

        final Handler ui = new Handler(Looper.getMainLooper());
        new Thread(new Runnable() {
            private MaterialDialog mDialog;

            @Override
            public void run() {
                ui.post(new Runnable() {
                    @Override
                    public void run() {
                        if (asText || mCurrentlyOpenLog == null || includeDeviceInfo) {
                            MaterialDialog.Builder progressDialog = new MaterialDialog.Builder(LogcatActivity.this);
                            progressDialog.title(R.string.dialog_please_wait);
                            progressDialog.content(getString(R.string.dialog_compiling_log));
                            progressDialog.progress(true, 0);
                            mDialog = progressDialog.show();
                            mDialog.setCanceledOnTouchOutside(false);
                            mDialog.setCancelable(false);
                        }
                    }
                });
                final SendLogDetails sendLogDetails = getSendLogDetails(asText, includeDeviceInfo);
                ui.post(new Runnable() {
                    @Override
                    public void run() {
                        startChooser(LogcatActivity.this, sendLogDetails.getSubject(), sendLogDetails.getBody(),
                                sendLogDetails.getAttachmentType(), sendLogDetails.getAttachment());
                        if (mDialog != null && mDialog.isShowing()) {
                            mDialog.dismiss();
                        }
                        if (asText && sendLogDetails.getBody().length() > 100000) {
                            Snackbar.make(findViewById(android.R.id.content), getString(R.string.as_text_not_work), Snackbar.LENGTH_LONG).show();
                        }
                    }
                });
            }
        }).start();

    }
 
Example #30
Source File: LogcatActivity.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
protected void verifyDelete(final CharSequence[] filenameArray,
                            final boolean[] checkedItems, final DialogInterface parentDialog) {

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    int deleteCount = 0;

    for (boolean checkedItem : checkedItems) {
        if (checkedItem) {
            deleteCount++;
        }
    }


    final int finalDeleteCount = deleteCount;

    if (finalDeleteCount > 0) {

        builder.setTitle(R.string.delete_saved_log)
                .setCancelable(true)
                .setMessage(getResources().getQuantityString(R.plurals.are_you_sure, finalDeleteCount))
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // ok, delete

                        for (int i = 0; i < checkedItems.length; i++) {
                            if (checkedItems[i]) {
                                SaveLogHelper.deleteLogIfExists(filenameArray[i].toString());
                            }
                        }

                        String toastText = getResources().getQuantityString(R.plurals.files_deleted, finalDeleteCount);
                        Toast.makeText(LogcatActivity.this, toastText, Toast.LENGTH_SHORT).show();

                        dialog.dismiss();
                        parentDialog.dismiss();

                    }
                });
        builder.setNegativeButton(android.R.string.cancel, null);
        builder.show();
    }


}