Skip to content

Commit

Permalink
move MMKV init & destry logic into Application class
Browse files Browse the repository at this point in the history
  • Loading branch information
lingol committed Apr 3, 2020
1 parent d44a767 commit 8f0dfe1
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 73 deletions.
3 changes: 2 additions & 1 deletion Android/MMKV/mmkvdemo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/AppTheme"
android:name=".MyApplication">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,47 +32,23 @@
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.getkeepsafe.relinker.ReLinker;
import com.tencent.mmkv.MMKV;
import com.tencent.mmkv.MMKVContentChangeNotification;
import com.tencent.mmkv.MMKVHandler;
import com.tencent.mmkv.MMKVLogLevel;
import com.tencent.mmkv.MMKVRecoverStrategic;
import com.tencent.mmkv.NativeBuffer;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import org.jetbrains.annotations.Nullable;

public class MainActivity extends AppCompatActivity implements MMKVHandler, MMKVContentChangeNotification {
public class MainActivity extends AppCompatActivity {
static private final String KEY_1 = "Ashmem_Key_1";
static private final String KEY_2 = "Ashmem_Key_2";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// set root dir
//String rootDir = MMKV.initialize(this);
String dir = getFilesDir().getAbsolutePath() + "/mmkv";
String rootDir = MMKV.initialize(dir, new MMKV.LibLoader() {
@Override
public void loadLibrary(String libName) {
ReLinker.loadLibrary(MainActivity.this, libName);
}
}, MMKVLogLevel.LevelInfo);
Log.i("MMKV", "mmkv root: " + rootDir);

// set log level
MMKV.setLogLevel(MMKVLogLevel.LevelInfo);

// you can turn off logging
//MMKV.setLogLevel(MMKVLogLevel.LevelNone);

MMKV.registerHandler(this);
MMKV.registerContentChangeNotify(this);

TextView tv = (TextView) findViewById(R.id.sample_text);
String rootDir = MMKV.getRootDir();
tv.setText(rootDir);

final Button button = findViewById(R.id.button);
Expand Down Expand Up @@ -141,12 +117,6 @@ public void onClick(View v) {
//testFastRemoveCornerSize();
}

@Override
protected void onDestroy() {
super.onDestroy();
MMKV.onExit();
}

private void testInterProcessLogic() {
MMKV mmkv = MMKV.mmkvWithID(MyService.MMKV_ID, MMKV.MULTI_PROCESS_MODE, MyService.CryptKey);
mmkv.putInt(MyService.CMD_ID, 1024);
Expand Down Expand Up @@ -456,44 +426,4 @@ private void testFastRemoveCornerSize() {
mmkv.removeValueForKey(key);
}
}

@Override
public MMKVRecoverStrategic onMMKVCRCCheckFail(String mmapID) {
return MMKVRecoverStrategic.OnErrorRecover;
}

@Override
public MMKVRecoverStrategic onMMKVFileLengthError(String mmapID) {
return MMKVRecoverStrategic.OnErrorRecover;
}

@Override
public boolean wantLogRedirecting() {
return true;
}

@Override
public void mmkvLog(MMKVLogLevel level, String file, int line, String func, String message) {
String log = "<" + file + ":" + line + "::" + func + "> " + message;
switch (level) {
case LevelDebug:
Log.d("redirect logging MMKV", log);
break;
case LevelNone:
case LevelInfo:
Log.i("redirect logging MMKV", log);
break;
case LevelWarning:
Log.w("redirect logging MMKV", log);
break;
case LevelError:
Log.e("redirect logging MMKV", log);
break;
}
}

@Override
public void onContentChangedByOuterProcess(String mmapID) {
Log.i("content changed", mmapID);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.tencent.mmkvdemo;

import android.app.Application;
import android.util.Log;

import com.getkeepsafe.relinker.ReLinker;
import com.tencent.mmkv.MMKV;
import com.tencent.mmkv.MMKVContentChangeNotification;
import com.tencent.mmkv.MMKVHandler;
import com.tencent.mmkv.MMKVLogLevel;
import com.tencent.mmkv.MMKVRecoverStrategic;

public class MyApplication extends Application implements MMKVHandler, MMKVContentChangeNotification {
@Override
public void onCreate() {
super.onCreate();

// set root dir
//String rootDir = MMKV.initialize(this);
String dir = getFilesDir().getAbsolutePath() + "/mmkv";
String rootDir = MMKV.initialize(dir, new MMKV.LibLoader() {
@Override
public void loadLibrary(String libName) {
ReLinker.loadLibrary(MyApplication.this, libName);
}
}, MMKVLogLevel.LevelInfo);
Log.i("MMKV", "mmkv root: " + rootDir);

// set log level
MMKV.setLogLevel(MMKVLogLevel.LevelInfo);

// you can turn off logging
//MMKV.setLogLevel(MMKVLogLevel.LevelNone);

MMKV.registerHandler(this);
MMKV.registerContentChangeNotify(this);
}

@Override
public void onTerminate() {
MMKV.onExit();

super.onTerminate();
}

@Override
public MMKVRecoverStrategic onMMKVCRCCheckFail(String mmapID) {
return MMKVRecoverStrategic.OnErrorRecover;
}

@Override
public MMKVRecoverStrategic onMMKVFileLengthError(String mmapID) {
return MMKVRecoverStrategic.OnErrorRecover;
}

@Override
public boolean wantLogRedirecting() {
return true;
}

@Override
public void mmkvLog(MMKVLogLevel level, String file, int line, String func, String message) {
String log = "<" + file + ":" + line + "::" + func + "> " + message;
switch (level) {
case LevelDebug:
Log.d("redirect logging MMKV", log);
break;
case LevelNone:
case LevelInfo:
Log.i("redirect logging MMKV", log);
break;
case LevelWarning:
Log.w("redirect logging MMKV", log);
break;
case LevelError:
Log.e("redirect logging MMKV", log);
break;
}
}

@Override
public void onContentChangedByOuterProcess(String mmapID) {
Log.i("content changed", mmapID);
}
}

0 comments on commit 8f0dfe1

Please sign in to comment.