summaryrefslogtreecommitdiff
path: root/java/org/gnu/emacs/EmacsPreferencesActivity.java
blob: 766e2e11d464b837dea8afc63cd5c2abd6ef4ea7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* Communication module for Android terminals.  -*- c-file-style: "GNU" -*-

Copyright (C) 2023-2024 Free Software Foundation, Inc.

This file is part of GNU Emacs.

GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */

package org.gnu.emacs;

import java.io.File;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;
import android.os.Build;

import android.widget.Toast;

import android.preference.*;

/* This module provides a ``preferences'' display for Emacs.  It is
   supposed to be launched from inside the Settings application to
   perform various actions, such as starting Emacs with the ``-Q''
   option, which would not be possible otherwise, as there is no
   command line on Android.

   This file extends a deprecated preferences activity, but no suitable
   alternative exists that is identical in appearance to system settings
   forms.  */

@SuppressWarnings ("deprecation")
public class EmacsPreferencesActivity extends PreferenceActivity
{
  /* Restart Emacs with -Q.  Call EmacsThread.exit to kill Emacs now,
     and tell the system to start EmacsActivity with some parameters
     later.  */

  private void
  startEmacsQ ()
  {
    Intent intent;

    intent = new Intent (this, EmacsActivity.class);
    intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK
		     | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.putExtra ("org.gnu.emacs.STARTUP_ARGUMENT", "--quick");
    startActivity (intent);
    System.exit (0);
  }

  /* Restart Emacs with `--debug-init'.  Call EmacsThread.exit to kill
     Emacs now, and tell the system to EmacsActivity with some
     parameters later.  */

  private void
  startEmacsDebugInit ()
  {
    Intent intent;

    intent = new Intent (this, EmacsActivity.class);
    intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK
		     | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.putExtra ("org.gnu.emacs.STARTUP_ARGUMENT", "--debug-init");
    startActivity (intent);
    System.exit (0);
  }

  /* Erase Emacs's dump file.  */

  private void
  eraseDumpFile ()
  {
    String wantedDumpFile;
    File file;
    Toast toast;

    wantedDumpFile = ("emacs-" + EmacsNative.getFingerprint ()
		      + ".pdmp");
    file = new File (getFilesDir (), wantedDumpFile);

    if (file.exists ())
      file.delete ();

    /* Make sure to clear EmacsApplication.dumpFileName, or
       starting Emacs without restarting this program will
       make Emacs try to load a nonexistent dump file.  */
    EmacsApplication.dumpFileName = null;

    /* Display a message stating that the dump file has been
       erased.  */
    toast = Toast.makeText (this, "Dump file removed",
			    Toast.LENGTH_SHORT);
    toast.show ();
  }

  @Override
  public final void
  onCreate (Bundle savedInstanceState)
  {
    Preference tem;
    Preference.OnPreferenceClickListener listener;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
      setTheme (android.R.style.Theme_DeviceDefault_Settings);
    else if (Build.VERSION.SDK_INT
	     >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
      setTheme (android.R.style.Theme_DeviceDefault);

    /* This must come before using any preference APIs.  */
    super.onCreate (savedInstanceState);

    /* Add preferences from the XML file where they are defined.  */
    addPreferencesFromResource (R.xml.preferences);

    /* Now, set up on click handlers for each of the preferences
       items.  */

    tem = findPreference ("start_quick");
    listener = new Preference.OnPreferenceClickListener () {
	@Override
	public boolean
	onPreferenceClick (Preference preference)
	{
	  startEmacsQ ();
	  return true;
	}
      };

    tem.setOnPreferenceClickListener (listener);
    tem = findPreference ("start_debug_init");
    listener = new Preference.OnPreferenceClickListener () {
	@Override
	public boolean
	onPreferenceClick (Preference preference)
	{
	  startEmacsDebugInit ();
	  return true;
	}
      };

    tem.setOnPreferenceClickListener (listener);
    tem = findPreference ("erase_dump");
    listener = new Preference.OnPreferenceClickListener () {
	@Override
	public boolean
	onPreferenceClick (Preference preference)
	{
	  eraseDumpFile ();
	  return true;
	}
      };

    tem.setOnPreferenceClickListener (listener);
  }
};