Skip to content

Commit

Permalink
Merge f421f75 into e1a33b2
Browse files Browse the repository at this point in the history
  • Loading branch information
guperrot committed Nov 30, 2018
2 parents e1a33b2 + f421f75 commit 0c02441
Show file tree
Hide file tree
Showing 33 changed files with 1,097 additions and 291 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

### AppCenter

* **[Feature]** Allow users to set userId for crashes and error logs. This feature adds an API, but is not yet supported on the App Center backend.
* **[Fix]** Do not delete old logs when trying to add a log larger than the maximum storage capacity.
* **[Fix]** Fix error detection of `setMaxStorageSize` API if database uses custom page size.
* **[Fix]** Fix minimum storage size verification to match minimum possible value.
Expand Down
Expand Up @@ -8,6 +8,7 @@
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.microsoft.appcenter.analytics.AnalyticsTransmissionTarget;
import com.microsoft.appcenter.analytics.PropertyConfigurator;
Expand All @@ -30,7 +31,8 @@ public class CommonSchemaPropertiesActivity extends AppCompatActivity {
private enum PropertyName {
APP_NAME,
APP_VERSION,
APP_LOCALE
APP_LOCALE,
USER_ID
}

@Override
Expand Down Expand Up @@ -62,6 +64,10 @@ public void onItemSelected(AdapterView<?> adapterView, View view, int position,
methodName = "getAppLocale";
break;

case USER_ID:
methodName = "getUserId";
break;

default:
throw new IllegalStateException();
}
Expand Down Expand Up @@ -109,6 +115,18 @@ public void saveProperty(View view) {
case APP_LOCALE:
mPropertyConfigurator.setAppLocale(value);
break;

case USER_ID:

// TODO remove reflection once new APIs available in jCenter.
// mPropertyConfigurator.setUserId(value);
try {
Method method = PropertyConfigurator.class.getDeclaredMethod("setUserId", String.class);
method.invoke(mPropertyConfigurator, value);
} catch (Exception ignored) {
}
break;
}
Toast.makeText(this, R.string.property_saved, Toast.LENGTH_SHORT).show();
}
}
Expand Up @@ -38,6 +38,7 @@
import com.microsoft.appcenter.sasquatch.listeners.SasquatchPushListener;
import com.microsoft.appcenter.utils.async.AppCenterConsumer;

import java.lang.reflect.Method;
import java.util.UUID;

public class MainActivity extends AppCompatActivity {
Expand All @@ -48,6 +49,8 @@ public class MainActivity extends AppCompatActivity {

static final String TARGET_KEY = "target";

static final String USER_ID_KEY = "userId";

static final String APPCENTER_START_TYPE = "appCenterStartType";

static final String LOG_URL_KEY = "logUrl";
Expand Down Expand Up @@ -155,6 +158,12 @@ protected void onCreate(Bundle savedInstanceState) {
String startType = sSharedPreferences.getString(APPCENTER_START_TYPE, StartType.APP_SECRET.toString());
startAppCenter(getApplication(), startType);

/* Set user id. */
String userId = sSharedPreferences.getString(USER_ID_KEY, null);
if (userId != null) {
setUserId(userId);
}

/* Attach NDK Crash Handler after SDK is initialized. */
Crashes.getMinidumpDirectory().thenAccept(new AppCenterConsumer<String>() {

Expand Down Expand Up @@ -238,6 +247,16 @@ public void accept(Boolean succeeded) {
});
}

public static void setUserId(String userId) {
// TODO remove reflection once new APIs available in jCenter.
// AppCenter.setUserId(userId);
try {
Method method = AppCenter.class.getMethod("setUserId", String.class);
method.invoke(null, userId);
} catch (Exception ignored) {
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
Expand Down
Expand Up @@ -12,6 +12,7 @@
import android.os.FileObserver;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceGroup;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
Expand All @@ -30,6 +31,7 @@
import com.microsoft.appcenter.crashes.Crashes;
import com.microsoft.appcenter.distribute.Distribute;
import com.microsoft.appcenter.push.Push;
import com.microsoft.appcenter.sasquatch.BuildConfig;
import com.microsoft.appcenter.sasquatch.R;
import com.microsoft.appcenter.sasquatch.activities.MainActivity.StartType;
import com.microsoft.appcenter.sasquatch.eventfilter.EventFilter;
Expand All @@ -47,6 +49,7 @@
import static com.microsoft.appcenter.sasquatch.activities.MainActivity.LOG_URL_KEY;
import static com.microsoft.appcenter.sasquatch.activities.MainActivity.MAX_STORAGE_SIZE_KEY;
import static com.microsoft.appcenter.sasquatch.activities.MainActivity.TARGET_KEY;
import static com.microsoft.appcenter.sasquatch.activities.MainActivity.USER_ID_KEY;

public class SettingsActivity extends AppCompatActivity {

Expand Down Expand Up @@ -390,40 +393,23 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
return true;
}
});
initClickableSetting(R.string.app_secret_key, MainActivity.sSharedPreferences.getString(APP_SECRET_KEY, getString(R.string.app_secret)), new Preference.OnPreferenceClickListener() {
initEditText(R.string.app_secret_key, R.string.app_secret_title, APP_SECRET_KEY, getString(R.string.app_secret), new EditTextListener() {

@Override
public boolean onPreferenceClick(final Preference preference) {
final EditText input = new EditText(getActivity());
input.setInputType(InputType.TYPE_CLASS_TEXT);
input.setText(MainActivity.sSharedPreferences.getString(APP_SECRET_KEY, getString(R.string.app_secret)));
public void onSave(String value) {
if (!TextUtils.isEmpty(value)) {
setKeyValue(APP_SECRET_KEY, value);
Toast.makeText(getActivity(), String.format(getActivity().getString(R.string.app_secret_changed_format), value), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), R.string.app_secret_invalid, Toast.LENGTH_SHORT).show();
}
}

new AlertDialog.Builder(getActivity()).setTitle(R.string.app_secret_title).setView(input)
.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String appSecret = input.getText().toString();
if (!TextUtils.isEmpty(appSecret)) {
setKeyValue(APP_SECRET_KEY, appSecret);
Toast.makeText(getActivity(), String.format(getActivity().getString(R.string.app_secret_changed_format), appSecret), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), R.string.app_secret_invalid, Toast.LENGTH_SHORT).show();
}
preference.setSummary(MainActivity.sSharedPreferences.getString(APP_SECRET_KEY, null));
}
})
.setNeutralButton(R.string.reset, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String defaultAppSecret = getString(R.string.app_secret);
setKeyValue(APP_SECRET_KEY, defaultAppSecret);
Toast.makeText(getActivity(), String.format(getActivity().getString(R.string.app_secret_changed_format), defaultAppSecret), Toast.LENGTH_SHORT).show();
preference.setSummary(MainActivity.sSharedPreferences.getString(APP_SECRET_KEY, null));
}
})
.setNegativeButton(R.string.cancel, null)
.create().show();
return true;
@Override
public void onReset() {
String defaultAppSecret = getString(R.string.app_secret);
setKeyValue(APP_SECRET_KEY, defaultAppSecret);
Toast.makeText(getActivity(), String.format(getActivity().getString(R.string.app_secret_changed_format), defaultAppSecret), Toast.LENGTH_SHORT).show();
}
});

Expand Down Expand Up @@ -452,42 +438,46 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
return true;
}
});
initClickableSetting(R.string.target_id_key, MainActivity.sSharedPreferences.getString(TARGET_KEY, getString(R.string.target_id)), new Preference.OnPreferenceClickListener() {
initEditText(R.string.target_id_key, R.string.target_id_title, TARGET_KEY, getString(R.string.target_id), new EditTextListener() {

@Override
public boolean onPreferenceClick(final Preference preference) {
final EditText input = new EditText(getActivity());
input.setInputType(InputType.TYPE_CLASS_TEXT);
input.setText(MainActivity.sSharedPreferences.getString(TARGET_KEY, getString(R.string.target_id)));
public void onSave(String value) {
if (!TextUtils.isEmpty(value)) {
setKeyValue(TARGET_KEY, value);
Toast.makeText(getActivity(), String.format(getActivity().getString(R.string.target_id_changed_format), value), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), R.string.target_id_invalid, Toast.LENGTH_SHORT).show();
}
}

new AlertDialog.Builder(getActivity()).setTitle(R.string.target_id_title).setView(input)
.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String targetId = input.getText().toString();
if (!TextUtils.isEmpty(targetId)) {
setKeyValue(TARGET_KEY, targetId);
Toast.makeText(getActivity(), String.format(getActivity().getString(R.string.target_id_changed_format), targetId), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), R.string.target_id_invalid, Toast.LENGTH_SHORT).show();
}
preference.setSummary(MainActivity.sSharedPreferences.getString(TARGET_KEY, null));
}
})
.setNeutralButton(R.string.reset, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String defaultTargetId = getString(R.string.target_id);
setKeyValue(TARGET_KEY, defaultTargetId);
Toast.makeText(getActivity(), String.format(getActivity().getString(R.string.target_id_changed_format), defaultTargetId), Toast.LENGTH_SHORT).show();
preference.setSummary(MainActivity.sSharedPreferences.getString(TARGET_KEY, null));
}
})
.setNegativeButton(R.string.cancel, null)
.create().show();
return true;
@Override
public void onReset() {
String defaultTargetId = getString(R.string.target_id);
setKeyValue(TARGET_KEY, defaultTargetId);
Toast.makeText(getActivity(), String.format(getActivity().getString(R.string.target_id_changed_format), defaultTargetId), Toast.LENGTH_SHORT).show();
}
});

// TODO remove this check once new APIs available in jCenter
if (BuildConfig.FLAVOR.contains("project")) {
initEditText(R.string.user_id_key, R.string.user_id_title, USER_ID_KEY, null, new EditTextListener() {

@Override
public void onSave(String value) {
setKeyValue(USER_ID_KEY, value);
MainActivity.setUserId(value);
}

@Override
public void onReset() {
setKeyValue(USER_ID_KEY, null);
MainActivity.setUserId(null);
}
});
} else {
PreferenceGroup preference = (PreferenceGroup) findPreference(getString(R.string.miscellaneous_key));
preference.removePreference(findPreference(getString(R.string.user_id_key)));
}
initClickableSetting(R.string.clear_crash_user_confirmation_key, new Preference.OnPreferenceClickListener() {

@Override
Expand Down Expand Up @@ -628,6 +618,43 @@ public void run() {
});
}

private void initEditText(int key, final int title, final String preferencesKey, final String defaultValue, final EditTextListener listener) {
Preference preference = getPreferenceManager().findPreference(getString(key));
if (preference == null) {
Log.w(LOG_TAG, "Couldn't find preference for key: " + key);
return;
}
preference.setSummary(MainActivity.sSharedPreferences.getString(preferencesKey, defaultValue));
preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

@Override
public boolean onPreferenceClick(final Preference preference) {
final EditText input = new EditText(getActivity());
input.setInputType(InputType.TYPE_CLASS_TEXT);
input.setText(MainActivity.sSharedPreferences.getString(preferencesKey, defaultValue));

new AlertDialog.Builder(getActivity()).setTitle(title).setView(input)
.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
listener.onSave(input.getText().toString());
preference.setSummary(MainActivity.sSharedPreferences.getString(preferencesKey, defaultValue));
}
})
.setNeutralButton(R.string.reset, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
listener.onReset();
preference.setSummary(MainActivity.sSharedPreferences.getString(preferencesKey, defaultValue));
}
})
.setNegativeButton(R.string.cancel, null)
.create().show();
return true;
}
});
}

private void initCheckBoxSetting(int key, final int enabledSummary, final int disabledSummary, final HasEnabled hasEnabled) {
Preference preference = getPreferenceManager().findPreference(getString(key));
if (preference == null) {
Expand Down Expand Up @@ -664,7 +691,7 @@ private void initClickableSetting(int key, Preference.OnPreferenceClickListener
private void initClickableSetting(int key, String summary, Preference.OnPreferenceClickListener clickListener) {
Preference preference = getPreferenceManager().findPreference(getString(key));
if (preference == null) {
Log.w(LOG_TAG, "Couldn't find preference for key: " + key);
Log.w(LOG_TAG, "Couldn't find preference for key: " + getString(key));
return;
}
preference.setOnPreferenceClickListener(clickListener);
Expand All @@ -676,7 +703,7 @@ private void initClickableSetting(int key, String summary, Preference.OnPreferen
private void initChangeableSetting(@SuppressWarnings("SameParameterValue") int key, String summary, Preference.OnPreferenceChangeListener changeListener) {
Preference preference = getPreferenceManager().findPreference(getString(key));
if (preference == null) {
Log.w(LOG_TAG, "Couldn't find preference for key: " + key);
Log.w(LOG_TAG, "Couldn't find preference for key: " + getString(key));
return;
}
preference.setOnPreferenceChangeListener(changeListener);
Expand Down Expand Up @@ -730,5 +757,12 @@ private interface HasEnabled {

void setEnabled(boolean enabled);
}

private interface EditTextListener {

void onSave(String value);

void onReset();
}
}
}
5 changes: 5 additions & 0 deletions apps/sasquatch/src/main/res/values/settings.xml
Expand Up @@ -94,6 +94,9 @@
<string name="target_id_changed_format" tools:ignore="MissingTranslation">Target Id has been changed to %s successfully. Please close the application completely and relaunch again.</string>
<string name="target_id_invalid" tools:ignore="MissingTranslation">Target Id is not valid.</string>

<string name="user_id_key" tools:ignore="MissingTranslation">user_id_key</string>
<string name="user_id_title" tools:ignore="MissingTranslation">User Id</string>

<string name="application_info_key" tools:ignore="MissingTranslation">application_info</string>
<string name="application_info_title" tools:ignore="MissingTranslation">Application Information</string>

Expand Down Expand Up @@ -121,6 +124,8 @@
<string name="log_url_set_to_production" tools:ignore="MissingTranslation">Log URL currently set to the production.</string>
<string name="log_url_production" tools:ignore="MissingTranslation">production</string>

<string name="property_saved" tools:ignore="MissingTranslation">Property saved</string>

<string name="save" tools:ignore="MissingTranslation">Save</string>
<string name="reset" tools:ignore="MissingTranslation">Reset</string>
<string name="cancel" tools:ignore="MissingTranslation">Cancel</string>
Expand Down
3 changes: 3 additions & 0 deletions apps/sasquatch/src/main/res/xml/settings.xml
Expand Up @@ -107,6 +107,9 @@
<Preference
android:key="@string/target_id_key"
android:title="@string/target_id_title" />
<Preference
android:key="@string/user_id_key"
android:title="@string/user_id_title" />
<Preference
android:key="@string/clear_crash_user_confirmation_key"
android:title="@string/clear_crash_user_confirmation_title" />
Expand Down
8 changes: 8 additions & 0 deletions apps/sasquatch/src/projectDependency/res/values/strings.xml
@@ -1,4 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<string name="app_name" tools:ignore="MissingTranslation">Sasquatch Test</string>

<!-- TODO move this to main xml after SDK available in jCenter. -->
<string-array name="common_schema_properties" tools:ignore="MissingTranslation">
<item>App Name</item>
<item>App Version</item>
<item>App Locale</item>
<item>User Id</item>
</string-array>
</resources>

0 comments on commit 0c02441

Please sign in to comment.